Create a new CSV by not copying that column from the old CSV.
Remove empty columns from csv file in powershell
Hi, I am exporting SharePoint lists data to .csv file. Some of the lists have empty fields, so when we are exporting data to .csv file i can see empty columns like
Title Name Email
aa aa@tiedtlaw email .com
bb bb@tiedtlaw email .com
How can i remove Title column from the .csv file.
3 answers
Sort by: Most helpful
-
-
MotoX80 33,381 Reputation points
2022-09-17T13:01:35.223+00:00 $csv = import-csv -Path C:\temp\Input.csv $columns = $csv | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name' $remove = @() "" "Column Length" $columns | foreach { $testdata = $csv.$($_) # get all of the data for this column $testdata = (-join $testdata).trim() # remove whitespace $testdata = $testdata.Replace("`0","") # remove 0x00 "{0,-40} {1}" -f $_, $testdata.length if ($testdata.length -eq 0) { # anything other than blanks? $remove += $_ # remove this column } } "" "Removing these columns" $remove $csv | Select-Object -ExcludeProperty $remove -Property * | Export-Csv -Path C:\temp\Output.csv -NoTypeInformation
-
Shailaja Setty 6 Reputation points
2022-09-22T20:02:47.2+00:00 Thanks for your reply MotoX80. Actually i have Collection with all the columns(some empty) data in $Collection and I am trying to add it to csv file as below and I couldn't remove empty columns as stated.
$columns = $Collection[0].psobject.Properties.Name
$remove = @()
$columns | foreach {
$testdata = $Collection.$($) # get all of the data for this column
if (((-join $testdata).trim().length) -eq 0) { # anything other than blanks?
$remove += $ # remove this column
}
}
"Removing these columns"
$remove #When I check by placing debugger $remove has BlankFoo
$Collection | Select-Object -ExcludeProperty $remove -Property * | Export-Csv -Path C:\temp\Output.csv -NoTypeInformationPlease correct me if I'm doing anything wrong.