Powershell

How to Redirect the Output of a PowerShell to a File During its Execution

PowerShell commands or functions can be redirected or exported to a file. This data is stored in a file for later use. When a PowerShell output is redirected, it saves the data and creates a new file. Moreover, PowerShell has several commands that can be used to redirect the output to a file, which includes the “Redirect operator >” or the “Out-File” command.

This tutorial will cover a guide to resolve the mentioned problem.

How to Redirect PowerShell Output to a File During Execution?

Here, we have enlisted the major approaches to fix the mentioned query:

Method 1: Redirecting the Output of a PowerShell Using the Redirect Operator “>”

The Redirect operator “>” in PowerShell is utilized to redirect the output to a text file. It is very useful for storing the important configurations of the PowerShell.

In PowerShell, a single “>” redirect operator sends output to a text file and creates it. However, when double “>>” redirect operators are used, they only append the existing text.

Example:

In the given example, we will redirect the output of the “Systeminfo” cmdlet to a text file:

> Systeminfo > C:\New.txt

In the above code:

  • Systeminfo” command is used to get the system information.
  • >” single redirect operator is used here to redirect the PowerShell output to a new text file.
  • In the end, we have given a file path and name where the output will be stored.

Verification

Let’s verify the performed output redirection operation using the “Get-Content” command and specifying the file path as follows:

> Get-Content C:\New.txt

The output confirms that the PowerShell output was redirected to a file.

Method 2: Redirecting the Output of a PowerShell Using “Out-File” Cmdlet

Another cmdlet can be used for the stated query. is the “Out-File” cmdlet. This cmdlet not only redirects the output of the PowerShell to a file, but it creates a new text file.

Example

In this example, first, use the “Get-Date” cmdlet to retrieve the system date. Then, the pipeline “|” takes the output from the previous command and then gives it as input to the next command. In the end, specify the location of the output file:

> Get-Date | out-file C:\File.txt

Again, execute the “Get-Content” command for the verification:

> Get-Content C:\File.txt

It can be observed that the current date has been stored in the mentioned file.

Conclusion

There are two methods for redirecting PowerShell output to a file. The first is using the “Redirect operator >” and the second is utilizing the “Out-File” command. Both of the methods not only output PowerShell data to a file but it creates a new file. This post has elaborated several methods to output PowerShell data to a file.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.