Description
If the first column header in a csv file or from csv data begins with a #
or with "#
, then both ConvertFrom-Csv
and Import-Csv
do not parse the file or data correctly. If the second row of the input (i.e., the first row of data) is blank, then the commands fail to parse the data correctly.
While the problem is particularly highlighted by the row of empty data, I believe the actual problem is just that these commands assume that if the first line of input begins with #
or "#
that it must be a type information line.
By type information line, I'm referring to the first line of output of these commands:
PS C:\> Get-ChildItem C:\Windows\ | Select-Object Name, LastWriteTime -First 1 | ConvertTo-Csv -IncludeTypeInformation
#TYPE Selected.System.IO.DirectoryInfo
"Name","LastWriteTime"
"addins","9/15/2018 3:33:54 AM"
This issue was first identified in this StackOverflow question.
Steps to reproduce
Example 1:
$data = @'
#,Labels,Machine Name,System Description,IP,User Logged,Disk Free C
,,,,,,
6,"computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader",PRO-MEDASST,91-0-928,172.10.201.111,CHR\\jeniferc,6.3
7,Update Adobe Reader,RED,empty,172.10.201.5,,9.5
8,Update Adobe Reader,SIER,empty,172.10.201.5,,6.8
'@
$data | ConvertFrom-Csv
Example 2:
$data = @'
"#",Labels,Machine Name,System Description,IP,User Logged,Disk Free C
,,,,,,
6,"computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader",PRO-MEDASST,91-0-928,172.10.201.111,CHR\\jeniferc,6.3
7,Update Adobe Reader,RED,empty,172.10.201.5,,9.5
8,Update Adobe Reader,SIER,empty,172.10.201.5,,6.8
'@
$data | ConvertFrom-Csv
This next example which doesn't have a row of empty data fields in the CSV also does not parse correctly, though it behaves differently. It uses the first row of data as the headers.
Example 3:
$data = @'
#,Labels,Machine Name,System Description,IP,User Logged,Disk Free C
6,"computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader",PRO-MEDASST,91-0-928,172.10.201.111,CHR\\jeniferc,6.3
7,Update Adobe Reader,RED,empty,172.10.201.5,,9.5
8,Update Adobe Reader,SIER,empty,172.10.201.5,,6.8
'@
$data | ConvertFrom-Csv
If the contents of data.csv
are the same as the value of the $data
above, then this also reproduces the error:
Import-Csv data.csv
Note that removing the line of blank values does not change the behavior.
Expected behavior
The commands should know that the first line is not a type information line, and should try to parse it as the header row for the CSV data.
# :
Labels :
Machine Name :
System Description :
IP :
User Logged :
Disk Free C :
# : 6
Labels : computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader
Machine Name : PRO-MEDASST
System Description : 91-0-928
IP : 172.10.201.111
User Logged : CHR\\jeniferc
Disk Free C : 6.3
# : 7
Labels : Update Adobe Reader
Machine Name : RED
System Description : empty
IP : 172.10.201.5
User Logged :
Disk Free C : 9.5
# : 8
Labels : Update Adobe Reader
Machine Name : SIER
System Description : empty
IP : 172.10.201.5
User Logged :
Disk Free C : 6.8
Actual behavior
Examples 1 & 2 do this:
WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.
H1
--
6
7
8
Example 3 does this:
6 : 7
computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader : Update Adobe Reader
PRO-MEDASST : RED
91-0-928 : empty
172.10.201.111 : 172.10.201.5
CHR\\jeniferc :
6.3 : 9.5
6 : 8
computers-PRO,Desktop,Office 2010,OS Windows 7,Update Adobe Reader : Update Adobe Reader
PRO-MEDASST : SIER
91-0-928 : empty
172.10.201.111 : 172.10.201.5
CHR\\jeniferc :
6.3 : 6.8
Environment data
Name Value
---- -----
PSVersion 6.2.1
PSEdition Core
GitCommitId 6.2.1
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Unfortunately, I do not currently have access to a system where I can install PowerShell 7.0 previews to test on.