Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Export-Csv to Write-Output

How can I convert an object into a series of CSV strings and output the strings to another command?

The Export-Csv cmdlet will only output to a file. I want to send the CSV output to another command or script output.

As a workaround, I could use Export-Csv to a file, read the file, then delete the file, but that approach has too much unnecessary overhead.

Export-Csv

Converts objects into a series of comma-separated value (CSV) strings and saves the strings to a file.

like image 853
Steven Avatar asked Oct 17 '25 08:10

Steven


2 Answers

You should use the ConvertTo-CSV cmdlet if you want to convert objects to CSV and return the result to stdout, instead of a file.

The ConvertTo-CSV cmdlet returns a series of comma-separated value (CSV) strings that represent the objects that you submit. You can then use the ConvertFrom-Csv cmdlet to recreate objects from the CSV strings. The objects converted from CSV are string values of the original objects that contain property values and no methods.

You'll find most builtin Export- cmdlets have a ConvertTo- equivalent. Another good way to discover cmdlets is to use Get-Command with a wildcard:

~> Get-Command *csv

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           epcsv -> Export-Csv
Alias           ipcsv -> Import-Csv
Cmdlet          ConvertFrom-Csv                                    3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          ConvertTo-Csv                                      3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Export-Csv                                         3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Import-Csv                                         3.1.0.0    Microsoft.PowerShell.Utility
like image 134
Mark Wragg Avatar answered Oct 19 '25 07:10

Mark Wragg


Export-csv is a combination of ConvertTo-CSV piped to Out-file:

Ex:

Get-Process | Select-Object -property Name,ID,VM,PM | ConvertTo-CSV | Out-File test2.html

you can use:

$a = Get-Process | Select-Object -property Name,ID,VM,PM | ConvertTo-CSV
like image 33
TudorIftimie Avatar answered Oct 19 '25 07:10

TudorIftimie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!