top of page

powershell script to extract specific columns from a .csv file


You can use a simple PowerShell script to extract specified columns from a .csv file.

Change the directory to the one containing your source .csv file. List the headings used in the .csv file - in this example, 'PlayerID'; 'GS'; 'TeamID' and 'YearID'.

Then list the paths to the source file and the file to be generated with the exported columns.

PS C:\foofolder\csv> $Headers = @( "PlayerID" "GS" "TeamID" "YearID" # Add all other desired headers here "$((Get-Date).ToShortDateString())" # << Header for todays date e.g. 19/06/2018 ) Import-Csv -Path "C:\Foofolder\csv\Fielding.csv" | Select $Headers | Export-Csv -Path "C:\Foofolder\csv\ExportFile.csv" -Force -NoTypeInformation

The script will then be customized and ready to run. It will out a new .csv file with only the columns you're interested in.

Thanks to Francis Hagyard for posting this here. You can repeat the script for multiple .csv files and run it all at once in PowerShell.

Unlike the method in command prompt in demonstrated in the Tip of the Night for April 24, 2020, the PowerShell script can extract data from columns beyond the 31st column in the .csv file.


bottom of page