Import-CSV in powershell fails to treat as csv array when importing one value

MrE-2124 26 Reputation points
2022-06-13T16:37:15.97+00:00

I have tested this on
PSVersion 5.1.19041.1682
PSEdition Desktop

and

PSVersion 5.1.17763.2867
PSEdition Desktop

Can anyone mimic the following results? I think there is an issue with how powershell natively treats a one line csv file with the import-csv command.

$csv_location = "C:\Scripts\CSV"  
  
### Sort and remove duplicates and write to a temp.csv  
Import-Csv -Path $csv_location\new.csv | sort user -Unique | Export-Csv -Path $csv_location\temp.csv -NoTypeInformation  
  
### Retrieve temp file to make updates  
$temp = Import-CSV $csv_location\temp.csv  
  
Write-Host $temp.count  

If the new.csv has two value lines, the returned value is 2. With one value line, it cannot evaluate $temp.count and returns nothing. I think the devs need to evaluate this.

Attached is a sample file I am using. Please rename to new.csv as Microsoft does not permit .csv uploads and renames files uploaded(?). 210973-new.txt

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-06-13T18:58:28.257+00:00
    • If the CSV file is completely empty, the Import-CSV returns a $null. The "count" property should return a zero.
    • If the CSV file contains only a header row, the Import-CSV returns a $null. The "count" property should return a zero
    • If the CSV file contains a header row and one data row, the Import-CSV returns an object of type PSCustomObject. The "count" property should return nothing because that object type contains no "count" property and neither does its parent which is a System.Object.
    • If the CSV file contains one header row and two data rows, the Import-CSV returns an object of type Object[] (i.e., an array of objects). The "count" property will have a value of two.

    If your code depends on an object always having a "count" property then cast the variable to an appropriate type. E.g., [array]$temp


3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-06-13T18:03:10.913+00:00

    Hi @MrE-2124 ,

    instead of $temp.count you could use Import-Csv -Path .\Junk\new.csv | Measure-Object
    The value count is 1 with your example csv file.

    Or this is an option as well and shows 1:

    $temp = Import-Csv -Path .\Junk\new.csv | Measure-Object  
    $temp.Count  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-06-13T18:52:19.64+00:00

    Hi @MrE-2124 ,

    if you use this the BaseType of $temp is a System.Object :

    $temp = Import-Csv -Path .\Junk\new.csv  
    $temp.GetType()  
    $temp.Count  
    

    If you want an array (BaseType = System.Array) please use this and count will work correct:

    $temp = @(Import-Csv -Path .\Junk\new.csv)  
    $temp.GetType()  
    $temp.Count  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

  3. Rich Matheisen 47,901 Reputation points
    2022-06-13T19:38:57.097+00:00

    For what it's worth . . .

    Here's the input CSV:

    user  
    User1  
    User2  
    User1  
    

    Here's the code (after correcting the spelling of "arrray" on line 7:

    $csv_location = "C:\Junk"  
          
     ### Sort and remove duplicates and write to a temp.csv  
     Import-Csv -Path $csv_location\new.csv | sort user -Unique | Export-Csv -Path $csv_location\temp.csv -NoTypeInformation  
          
     ### Retrieve temp file to make updates  
     [array]$temp = Import-CSV $csv_location\temp.csv  
          
     Write-Host $temp.count  
          
     $temp[0].user  
    

    And here's the output:

    2  
    User1  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.