In the previous post, you learnt how to change the directory in PowerShell. And, in this tutorial, I will show you PowerShell output to file. You will see how to save the output of the command prompt or PowerShell script to a text or .csv file. For your knowledge, both the type of files, that is the text file and the .csv file are common file types for data exchange.
Windows PowerShell and Command Prompt are very powerful tools for system administrators; and even developers who have the ability to automate tasks and write command output to files. Especially when dealing with large content, it is not possible to copy and paste the output from the clipboard; into the file every time.
There are many different scenarios, such as – if you need to send data to a client, validate the data, or troubleshoot an issue, it is very handy to export the data to a file. The window gives us the option to save the output of the command to a file using PowerShell or Windows Command Prompt.
PowerShell Output to File
In this section, hereon, we will look at how to output as a .txt file.
PowerShell output to a text file
Following are the steps to save the command output in a text file using PowerShell:
- Open Start.
- Search for PowerShell and select Run as administrator.
- Now, make use of the given command to save your output to a text datatype file.
C:\ > ipconfig | out-file -FilePath "D:\Shubham\output.txt"
Note, that in this example when you use the command out-file , the output of the ipconfig command is exported to the output.txt file.
You need to make sure that the file path you specified in the command exists, otherwise the “Could not find the path” exception occurs.
Also, remember that if you want to read the contents of the exported file, use the get-content command, which reads the contents of the file.
C:\ > get-content "D:\Shubham\output.txt"
PowerShell output to a .csv file
Once we have the output in table format, we export it to a CSV file. Writing the output to a CSV file is the same as writing a text file, except you use the .csv extension instead of .txt.
C:\Windows\system32 > get-process | out-file "D:\Shubham\output.csv"
Here we see an example wherein we retrieve the local Windows processes using the command “get-process”. Thereater, we save the output of this file as a .csv file.
To save the command output to a file, follow the steps below:
- Open the Start menu.
- Locate Command Prompt, right-click it, and select Run as administrator.
- Now, type the following command to save the output to a text file and press Enter.
- Command >
- Where “command” represents what you want to save to the file, and “file path” represents where the output will be saved.
C:\windows\system32 > ipconfig > "D:\Shubham\my_output.txt"
- One problem with this is that this command always overwrites the existing file contents. But, it may be that you want to append the output to a file.
Appending the output to an existing file
If you do not want to remove or overwrite the existing data in the existing file; then the -append parameter is used before the file name, as shown in the command below :
C: > ping 1.1.1 | out-file -append -FilePath "D:\Shubham\my_output.txt"
The above command appends the output of the ping command to the existing output.txt file without overwriting the existing data.
SUMMING UP !!
In the earlier post, you saw how to change the directory in PowerShell. Now, in this post, you have seen how to save the outputs you generate in a file say for example a text file or a csv file for that matter too. You have seen and learnt about the same using ample examples. With this take, until next time, see ya!! Goodbye !! :)~~