In this tutorial, I show you how we can save command prompt or PowerShell script output to text or a .csv file.
Both text file and .csv file is a very common file type to exchange data.
Windows PowerShell and Command Prompt are very powerful tools for system administrators and developers which have the ability to automate the task and write command output to files.
It’s not possible especially in the case of large content you copy the output from the clipboard every time and paste into the file. There are many different scenarios like – send data to the client, need to validate the data, or troubleshoot any issue it is very convenient to export the data into a file. The window gives us options to save the output of the command to file using PowerShell or Windows command prompt.
Table of content
- Write Command output to a text file using Powershell.
- Write output to CSV file using PowerShell.
- Save command output to file using Command Prompt.
- Append Output to a text file using PowerShell.
Below are the steps to save the command output to a text file using PowerShell.
1. Write Command output to a text file using Powershell
To save command output to a text file using PowerShell, follow the below steps:
a) Open Start.
b) Search for PowerShell, and select the Run as administrator option.
c) Use the following command to save the output to a text file and press enter.
C:\> ipconfig | Out-File -FilePath "D:\geekstutorials\output.txt"
In this example using the “Out-File” command, the “ipconfig” command output exported in the output.txt file.
Make sure the file path you mentioned in the command must exist otherwise “Couldn’t find the path” exception occurred.
And if you want to read the content of the exported file use the “Get-Content” command which reads the file content.
C:\> Get-Content "D:\geekstutorials\output.txt"
2. Write output to CSV file using PowerShell
When we have output in table format then we export in CSV file. Write output to CSV file is same as text file just use the .csv extension instead of .txt.
PS C:\Windows\system32> Get-Process | Out-File "D:\geekstutorials\outfile.csv"
Here I have fetched the local windows processes using the “Get-Process” command and saved the output into a .csv file.
The output looks like below –
3. Save command output to file using Command Prompt
To save command output to file follow the below steps. In this example, I save the output to a text file.
a) Open the Start menu.
b)Search for Command Prompt, right-click on it and select the Run as administrator option.
c) Type the following command to save the output to a text file and hit the enter button.
command > <filepath>
Here the “command” is what you want to save in the file and “filepath” is the location where the output gets saved.
C:\windows\system32> ipconfig > “D:\geekstutorials\output.txt”
Note – Make sure the path should exist otherwise an exception has occurred on the command prompt.
This very helpful when we have large output and need to validate later as it gets saved in a file.
Here the problem is all the above commands always overwrite the existing file content what if you want to append the output to the file.
4. Append output to an existing file using PowerShell
If you do not want to remove or overwrite the existing data in the existing file, the –Append parameter is used before the file name as shown in the below command.
PS C:>ping 18.104.22.168 | Out-File -Append -FilePath "D:\geekstutorials\output.txt"
The above command appends the ping command output to the existing output.txt file without overwriting it’s existing data.
In this article, I explained how we save the command output or PowerShell output to text and CSV file using command prompt and Windows PowerShell. Also, we see how we can append the output to the existing file without overwriting its content.