Remove empty columns from csv file in powershell

Shailaja Setty 6 Reputation points
2022-09-16T22:25:25.843+00:00

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.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,536 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 46,796 Reputation points
    2022-09-17T01:51:17.623+00:00

    Create a new CSV by not copying that column from the old CSV.

    0 comments No comments

  2. MotoX80 33,921 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  
    
    
      
    
      
    

  3. 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 -NoTypeInformation

    Please correct me if I'm doing anything wrong.


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.