PowerShell is a command-line shell designed particularly for system administrators. It helps administrators to manage and automate the administration of Windows operating systems and the Apps running on it. PowerShell has the ability to save the outputs of commands you run in it into a file which you can later view, analyze, and share it with someone else for troubleshooting.
In this article, I will demonstrate the steps to save command output to a file. In the windows search bar, type powershell.exe and press Enter on the keyboard to launch PowerShell.
Send output to file in PowerShell
In the PowerShell window, enter the command followed by Out-File -FilePath .\filename.txt
Command | Out-File -FilePath .\FileName.txt
Replace the Command, FilePath, and FileName parameters with your own command, file path, and names simultaneously. If the file does not exist, Out-File will create the file with name and path specified.
For instance, I want to output the ipconfig result to a file named ip.txt and save it on the desktop. The command will be:
ipconfig | Out-File -FilePath desktop\ip.txt
Running the above command will save the output of ipconfig as a file ip.txt on desktop. If a file already exists with some content, Out-file will overwrite it.
Prevent overwriting of output file
To prevent already existing file from being overwritten, add -NoClobber parameter after the above command.
ping | Out-File -FilePath desktop\ip.txt -NoClobber
-NoClobber will prevent overwriting and displays a message that file already exist.
Append the output to an existing file
If you want to add another output in the same file and also do not want to remove or overwrite the data, add –Append parameter before the file name as shown in the below command.
Ping 188.8.131.52 | Out-File -Append -FilePath desktop\ip.txt
The above command will add the output of Ping command in the same text file without overwriting it.
In this article, I described the method to save the PowerShell commands output in a file. You can save the output of the commands in a text file, prevent it from overwriting, and to add the output of another command in the same file.