Getting data in and out of PowerShell is a critical part of using the language. There are a number of commands dedicated to this task by default.
The Export-Csv
command writes data from objects to a text file, for example:
Get-Process | Export-Csv processes.csv
By default, Export-Csv
will write a comma-delimited file using ASCII encoding and will completely overwrite any file using the same name.
Export-Csv
may be used to add lines to an existing file using the Append
parameter. When the Append
parameter is used, the input object must have each of the fields listed in the CSV header or an error will be thrown unless the Force
parameter is used:
PS> Get-Process powershell | Select-Object Name, Id | Export-Csv .\Processes.csv Get-Process explorer | Select-Object Name | Export-Csv .\Processes.csv -Append Export-Csv : Cannot append CSV content to the following file: .\Processes.csv. The appended object does not have a property that corresponds...