Many times we need to run commands for various purposes from windows command prompt. The output of the command depends on command type. If the output is very large then we need to scroll the window to find out the desired information which is very annoying. There are some commands that provide important information and sometimes we need to share this information with our technical stuffs or use on other documents. Taking the screenshot of the output is not a good choice and for the large output all outputs can’t be taken as single screenshot image. Another problem of screenshot is that if we want to copy some text from the command output for other use then it is not possible to do from image. So if the command output can be saved as a text file then it will be very helpful for the user to access content easily. How you can save or redirect the command prompt output to a text file on windows operating system is shown in this article.
To save any command output from the command prompt, you have to use the redirect operator (single angle bracket) “>” and the command using the following syntax:
Command > Filename
Suppose we want to find out the directory list of any drive of your computer and save the output into a text file name directorylist.txt on D: drive. If the file does not exist then the new file will be created and if the file exists then the command output will overwrite the file content. Type the ‘dir’ command in the following way to save the output on output.txt file.
dir > d:\output.txt
Now open D: drive and search the file output.txt. If the file exists then double click on the file to open it.
If we the check the content of the file then it will be found that both command prompt output and file content are same.
If we want to run multiple commands in the command prompt and save it in a file then last command output will be overwritten every time. If we run dir and ping commands one after another then the text file contains only the last output.
dir > d:\output.txt
ping google.com > d:\output.txt
To solve this problem, we need to append the command outputs in the redirecting file. We have to use double angle bracket “>>” to append the output.
ping hotmail.com > d:\output2.txt
ping yahoo.com >> d:\output2.txt
Now if we open the output2.txt file then it will show both two command outputs.
When we want to save the redirected file in a particular drive and the error arrives or file not creates. This means you have no permission to write on this drive. This error occurs for that drive where Windows is installed. By default, we install Windows in “C:” drive. So when we want to create any file in drive C: we will get the access permission error.
We have to open the command prompt window with administrator permission to solve this problem. Right click on the cmd option and select “Run as Administrator” from the pop up menu.
Now if we redirect the output of the commands into the text file then the file will be created.
In this way, we can save any number of command outputs to a text file for accessing information easily or sharing the information with others when requires.