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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With