I am importing this CSV file into powershell but it is not importing the headers and also not all the columns. It is only getting the first column but thinks there are no headers. I realize the second line is empty but that is how the software generates the CSV file with the data I need to work with.
CSV File:
#,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
Powershell Code:
$computerscsv = import-csv -Path "C:\C Drives Less than 10GB free.csv" #import the csv file
$computerscsv | Format-Table
pause
Powershell Output:
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
Any help would be greatly appreciated.
Edit: This issue has been reported on the PowerShell GitHub repository.
It appears to be a bug in the command. The header in the CSV file can't begin with an #
. Note that converting the first header to "#"
instead of #
also doesn't fix the issue.
As a workaround:
$data = Get-Content "C:\C Drives Less than 10GB free.csv"
$data[0] = 'Num' + $data[0]
$data | ConvertFrom-Csv
Or, for a more general solution:
$data = Get-Content "C:\C Drives Less than 10GB free.csv"
if ($data[0][0] -eq '#') {
$data[0] = 'Num' + $data[0]
}
elseif ($data[0].Substring(0,2) -eq '"#') {
$data[0] = '"Num' + $data[0].Substring(1)
}
$data | ConvertFrom-Csv
My guess is that Import-Csv
and ConvertFrom-Csv
are mistaking the first line beginning with a #
as the start of the (now somewhat deprecated) type information line:
PS C:\> Get-ChildItem C:\Windows\ | Select-Object Name, LastWriteTime -First 1 | ConvertTo-Csv -NoTypeInformation:$false
#TYPE Selected.System.IO.DirectoryInfo
"Name","LastWriteTime"
"addins","9/15/2018 3:33:54 AM"