home

PowerShell Import-csv Export-csv Get-Content ConvertFrom-csv
Author Nigel Rivett

For the following create a file with extension .bat, copy the code into the file and run it or run from command window.
For a .bat add set /p var=press enter to the end so the window doesn't close


Read first 10 rows from a file and output to another file. Handy to get few rows from large file o open in notepad or excel
powershell "get-content c:\test\myfile.csv -head 10 > c:\test\outfile.csv"


view data from file formatted into columns
powershell "get-content c:\test\myfile.csv | convertfrom-csv"


display formatted data from the middle of a file
powershell "Import-csv c:\test\myfile.csv | select -fist 20 | select -last 5"
powershell "(Import-csv c:\test\myfile.csv)[15,20]"


You can pipe get-content but need the header
powershell "get-content c:\test\myfile.csv -head 10 | ConvertFrom-csv"
powershell "(get-content c:\test\myfile.csv)[0,15,16,17,18] | ConvertFrom-csv"


Select the columns to process
powershell "Import-csv c:\test\myfile.csv | select name, address | Export-csv -NoType -path c:\test\outfile.csv"


With a pipe delimiter
powershell "get-content | convertfrom-csv -delimiter '|' | select name, address | Export-csv -NoType -path c:\test\outfile.csv"

home