How to save command output to a file using PowerShell

PowerShell is a command-line shell designed specifically for system administrators. It helps administrators manage and automate the administration of Windows operating systems and the apps that run on them. PowerShell provides the ability to save the output of commands you run in it to a file that you can later view, analyze, and share with others for troubleshooting.

In this article, I'll show you how to save the 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 the 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

Send output to file in PowerShell

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.

Save output of ipconfig command to a file

Prevent overwriting of the output file

To prevent the already existing files 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.

Prevent overwriting of output file

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 8.8.8.8 | Out-File -Append -FilePath desktop\ip.txt

Append the output to an existing file

The above command will add the output of Ping command in the same text file without overwriting it.

Command output appended to file using PowerShell

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 add the output of another command in the same file. If you like to know how to find files on Windows command prompt, check out this guide.

Leave a Comment