The commands that you execute in PowerShell and Command Prompt can output information. Sometimes, it’s just a few lines e.g., when you check the ping, or it might be a lot of lines e.g. when your local IP address or your external IP. Regardless of how long the output is, sometimes you just need to save it to a file so that you don’t have to fetch the information again and again. Both Command Prompt and PowerShell support copy and paste but you can also save command output to a file when the command is run. Here’s how you can do that in both Command Prompt and PowerShell.
Command output to file – Command Prompt
Open Command Prompt and enter the command that you want to run. Before you tap Enter though, add the following at the end;
You don’t have to create the text file first. Command Prompt will do that for you. If you enter just the name of the file, it will be created in the same directory that the Command Prompt is currently in. You can save the output to a different location by adding the path to the folder you want to save the output to.
If you want to save the output to a file but also view it in Command Prompt, you can do that with this command;
> path-to-folder\name-of-file.txt | type path-to-folder\name-of-file.txt
Command output to file – PowerShell
In PowerShell, you can send command output to a file the same way you can in Command Prompt. Enter the command you want to run and before you tap Enter, add the following at the end. Again, you won’t have to create the text file. PowerShell will do that for you.
This will save the output file to same directory that PowerShell is currently in. To save the output to a file in a different location, you can specify the path to the folder where you want to save it.
If you want to view the output, you can’t do that at the same time that the file is being created. Once it has been created, run this command and you will be able to view its contents.
Get-Content -Path "path-to-file\file-name.txt"
If you’re wondering whether the output can be saved to a different type of file, the answer is no. Both Command Prompt and PowerShell can only natively write to/create text files and there’s no way to format the output so that when it’s saved to a file, it looks a certain way.