How to Write PowerShell Output to file

In this tutorial, I show you how we can save command prompt or PowerShell script output to text or a .csv file.

Both text file and .csv file is a very common file type to exchange data.

 How to Write PowerShell Output to file

Windows PowerShell and Command Prompt are very powerful tools for system administrators and developers which have the ability to automate the task and write command output to files.

It’s not possible especially in the case of large content you copy the output from the clipboard every time and paste into the file. There are many different scenarios like – send data to the client, need to validate the data, or troubleshoot any issue it is very convenient to export the data into a file. The window gives us options to save the output of the command to file using PowerShell or Windows command prompt.

Table of content

  1. Write Command output to a text file using Powershell.
  2. Write output to CSV file using PowerShell.
  3. Save command output to file using Command Prompt.
  4. Append Output to a text file using PowerShell.
  5. Conclusion

Below are the steps to save the command output to a text file using PowerShell.

1. Write Command output to a text file using Powershell

       To save command output to a text file using PowerShell, follow the below steps:

       a) Open Start.

       b) Search for PowerShell, and select the Run as administrator option.

       c) Use the following command to save the output to a text file and press enter.

     C:\> ipconfig | Out-File -FilePath "D:\geekstutorials\output.txt"

In this example using the “Out-File” command, the “ipconfig” command output exported in the output.txt file.

Make sure the file path you mentioned in the command must exist otherwise “Couldn’t find the path” exception occurred.

Write PowerShell command output to text file

And if you want to read the content of the exported file use the “Get-Content” command which reads the file content.

C:\> Get-Content "D:\geekstutorials\output.txt"

Write output to CSV file using PowerShell

 2. Write output to CSV file using PowerShell

   When we have output in table format then we export in CSV file. Write output to CSV file is same as text file                   just use the .csv extension instead of .txt.

PS C:\Windows\system32> Get-Process | Out-File "D:\geekstutorials\outfile.csv"

Here I have fetched the local windows processes using the “Get-Process” command and saved the output into a .csv file.

The output looks like below –

Powershell output to csv file

3. Save command output to file using Command Prompt

To save command output to file follow the below steps. In this example, I save the output to a text file.

     a) Open the Start menu.

     b)Search for Command Prompt, right-click on it and select the Run as administrator option.

     c) Type the following command to save the output to a text file and hit the enter button.

      command > <filepath>

Here the “command” is what you want to save in the file and filepath” is the location where the output gets saved.

Example –

C:\windows\system32> ipconfig > “D:\geekstutorials\output.txt”

Save command output to file using Command Prompt

Note – Make sure the path should exist otherwise an exception has occurred on the command prompt.

This very helpful when we have large output and need to validate later as it gets saved in a file.

Here the problem is all the above commands always overwrite the existing file content what if you want to append the output to the file.

4. Append output to an existing file using PowerShell

If you do not want to remove or overwrite the existing data in the existing file, the  –Append parameter is used before the file name as shown in the below command.

PS C:>ping 1.1.1.1 | Out-File -Append -FilePath "D:\geekstutorials\output.txt"


4.	Append output to an existing file using PowerShell

The above command appends the ping command output to the existing output.txt file without overwriting it’s existing data.

Conclusion

In this article, I explained how we save the command output or PowerShell output to text and CSV file using command prompt and Windows PowerShell. Also, we see how we can append the output to the existing file without overwriting its content.

3 thoughts on “How to Write PowerShell Output to file

  1. Good tutorial of outputting from execution of an existing PowerShell script.

    You could also add the “Start-Transcript” command at the top inside the script being executed to create the file by default whenever executed.

Leave a Reply

Your email address will not be published.