Output results from PowerShell to CSV or XML

Have you wanted to output results from PowerShell to CSV or XML? Well I am going to show you how.

I have previously explained how to export results of PowerShell queries but did not include how to export the results to CSV or XML. The reason for that is that when you become familiar with PowerShell you will find out that PowerShell commands can return a lot more information than what they show on screen. I will show how to get that information in a future blog post but just be aware that there is a lot more information readily available and only the displayed columns are exported to text(TXT) files.

When exporting to CSV and XML the hidden columns are exported as well. I will give an example of the difference in the exports.

The results of Get-Process are displayed below.

1

If I run the below commands I would output the results of Get-Process to CSV and XML.

Get-Process | Export-CSV c:\PowerShell\ProcessCSV.csv

Get-Process | Export-CliXML c:\PowerShell\ProcessCliXML.xml

2

When you look in the text file you will see the same results as shown on screen.

3

However, the CSV and XML have all the columns available for the results of that command. I have shown an excerpt of the Get-Process results in CSV format below.

4

I would advise that you use TXT, CSV and XML files where appropriate and check out the results you could get from various commands.

Simple way to output results to a file using PowerShell

Have you ever wanted to send the output of a PowerShell command to a file? Well I am going to show you how.

When you run a PowerShell command like Get-Process, Get-Host…. It will return useful information but sometimes you want to save that information for future reference. You could cut and paste or take a screenshot but these are way more complicated than simply using the inbuilt functionality within PowerShell.

For example if you wanted to output the processes currently running you would use Get-Process. To output it you would add the below command

Get-Process  >  yourfilename.txt

Get-Process > C:\PowerShell\Process.txt

1