Powershell remove empty Columns from CSV file

Kenneth Tan 1 Reputation point
2021-07-16T06:28:20.757+00:00

As part of an import-process, I'm trying to script the solution to prepare .XLSX files to managable CSV Files.
The part I'm not (yet) able to resolve in Powershell is the removal of empty Columns.

There are some rules to consider:

  • There is a header Row
  • The Header Row content is Random every time a CVS is generated (Customer .XLSX export file)

Solution:

  • Index the Header Row
  • Check columns content is empty (or 1, including the header)
  • Delete the Column(s)
  • Write CSV file.

Can someone help me, with this script.

I was able to Google-script the following other parts:

  • Convert XLSX files to multiple CVS-worksheets and combine them to 1 Master CSV.
  • Cleanup script, where I can pipe an array of RegEx replacements onto that Master CSV
  • Split the Master CSV Into manageable smaller CSV documents (Some of our CSV files have 1-2 million rows!)
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-07-16T08:38:18.783+00:00

    Hi,

    Please check to see if this works for you.

    $input = "C:\temp\input.csv"  
    $output = "C:\temp\output.csv"  
    $objs = Import-Csv -Path $input   
    $properties = ($objs | Get-Member -MemberType NoteProperty).Name  
    $exclude = @()  
    foreach($property in $properties){  
        if(($objs.$property | where-object{$_}).count -eq 0){  
            $exclude += $property   
        }  
    }  
    $objs | Select-Object -Property * -ExcludeProperty $exclude | Export-Csv -NoTypeInformation -Path $output  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.