0
votes

Powershell Script to Delete Blank Columns from CSV

I have a spread sheet which I'm importing into a MySQL database, the import fails because of blank columns in the spread sheet.

Is there a powershell script I can run / create that will check any given CSV file and remove blank columns?

Col1,Col2,Col3,Col4,,,, Val1,Val2,Val3,Val4

2
You should give an example of what you've got and what you need. A short example of what you test so far would be nice too. - JPBlanc
Example of what I have: Col1,Col2,Col3,Col4,,,, Val1,Val2,Val3,Val4 Example of what I want: Col1,Col2,Col3,Col4 Val1,Val2,Val3,Val4 - James

2 Answers

1
votes

How about something like this:

$x = Import-Csv YourFile.csv
$f = $x[0] | Get-Member -MemberType NoteProperty | Select name
$f | Add-Member -Name count -Type NoteProperty -Value 0
$f | %{
  $n = $_.Name
  $_.Count = @($x | Select $n -ExpandProperty $n | ? {$_ -ne ''}).count
}
$f = @($f | ? {$_.count -gt 0} | Select Name -expandproperty Name)

$x | Select $f | Export-Csv NewFile.csv -NoTypeInformation

It uses Get-Member to get the column names, cycles though each one to check how many are not blank and then uses the results in a select.

0
votes

When I run Dave Sexton's code, I get:

Select-Object : Cannot convert System.Management.Automation.PSObject to one of the following 
types {System.String, System.Management.Automation.ScriptBlock}.
At line:15 char:12
+ $x | Select <<<<  $f  | Export-Csv ColsRem.test.$time.csv -NoTypeInformation
+ CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
+ FullyQualifiedErrorId : 
                  DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand

I corrected this issue by adding one more line, to force each array element to be a string.

$x = Import-Csv YourFile.csv
$f = $x[0] | Get-Member -MemberType NoteProperty | Select name
$f | Add-Member -Name count -Type NoteProperty -Value 0
$f | %{
  $n = $_.Name
  $_.Count = @($x | Select $n -ExpandProperty $n | ? {$_ -ne ''}).count
}
$f = @($f | ? {$_.count -gt 0} | Select Name -expandproperty Name)

# I could get the select to work with strings separated by commas, but the array would 
# always produce the error until I added the following line, explicitly changing the 
#values to strings.
$f = $f | Foreach-Object { "$_" } 

$x | Select $f | Export-Csv NewFile.csv -NoTypeInformation

My import CSV contains a few hundred columns and about half likely won't be populated, so getting rid of the extra columns was necessary. Now I just need to figure out how to counteract the unintended re-ordering of the columns into alphabetical order by name, without changing the names.