Writing a CSV File
Let's learn how to write a CSV file with an example.
We'll cover the following
Using PowerShell cmdlet Export-CSV
PowerShell objects can simply be piped to the Export-CSV
cmdlet with a valid file name, which will convert the object into CSV format and write to the file. In the following example, we will first capture some services running on a Windows machine with specific properties and then export them to a file through the pipeline using the Export-CSV
cmdlet. The use of the -NoTypeInformation
switch parameter indicates that this cmdlet omits the type information from the CSV file, which is #TYPE
followed by the fully qualified name of the type of object in the first line of the CSV file.
Get-Service win* | Select-Object Name, Status | Export-Csv C:\Temp\Service.csv -NoTypeInformation
We can even convert a dictionary to PowerShell objects by casting it to the [PSCustomObject]
type accelerator. This will convert it into an object which we can then export to a CSV file using Export-CSV
cmdlet.
Note: Run the
cat Test.csv
command after hitting the “Run” button to see theTest.csv
file.
Get hands-on with 1400+ tech skills courses.