CSV cmdlets

I was reading something and when ConvertFrom-CSV was mentioned it made me pause and think about what that cmdlet actually did. This is a quick explanation of the CSV cmdlets.

Export-CSV was available in PowerShell v1. You use it to create a CSV file from PowerShell objects. The code in PowerShell v6.1 is

PS>  Get-Process -Name p*  | select Name, id, StartTime  | Export-Csv -Path C:\test\test1.csv

In Windows PowerShell v5.1 it would be

PS> Get-Process -Name p*  | select Name, id, StartTime  | Export-Csv -Path C:\test\test1.csv -NoTypeInformation

The –NoTypeInformation parameter is needed to prevent the type information being written to the first line of the CSV. In v6.1 the default is NOT to write type information to the csv file

Import-CSV again has been a staple of PowerShell since v1. You use this cmdlet to read a CSV file that you have saved on disk.

PS>  Import-Csv -Path C:\test\test1.csv
Name       Id    StartTime
----       --    ---------
powershell 14632 24/09/2018 12:27:17
pwsh       9804  24/09/2018 12:40:38

You can then send the objects down the pipeline for further processing.

The important thing to remember is that Import/Export-CSV deal with files.

ConvertTo-Csv is used to create CSV strings from objects. In PowerShell v6.1

PS>  Get-Process -Name p*  | select Name, id, StartTime | ConvertTo-Csv
"Name","Id","StartTime"
"powershell","14632","24/09/2018 12:27:17"
"powershell_ise","10968","24/09/2018 12:48:56"
"pwsh","9804","24/09/2018 12:40:38"

If you do the same in Windows PowerShell v5.1 you get

PS> Get-Process -Name p*  | select Name, id, StartTime | ConvertTo-Csv
#TYPE Selected.System.Diagnostics.Process
"Name","Id","StartTime"
"powershell","14632","24/09/2018 12:27:17"
"powershell_ise","10968","24/09/2018 12:48:56"
"pwsh","9804","24/09/2018 12:40:38"

So you need to use the –NoTypeInformation parameter again

PS> Get-Process -Name p*  | select Name, id, StartTime | ConvertTo-Csv -NoTypeInformation
"Name","Id","StartTime"
"powershell","14632","24/09/2018 12:27:17"
"powershell_ise","10968","24/09/2018 12:48:56"
"pwsh","9804","24/09/2018 12:40:38"

ConvertTo-Csv is analogous to Export-CSV except that the data is in memory rather than being on disk.

ConvertFrom-CSV reads CSV strings and converts them to objects

$p = @'
Name,Id,StartTime
powershell,14632,24/09/2018 12:27:17
pwsh,9804,24/09/2018 12:40:38
'@

ConvertFrom-Csv -InputObject $p

ConvertFrom-Csv is analogous to Import-CSV except that the data is in memory rather than being on disk.

The Import/Export cmdlets deal with data on disk while the ConvertTo/ConvertFrom cmdlets are for data that remains in memory.

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s