Powershell

Using PowerShell Out-file Cmdlet to Redirect Output to a File

PowerShell is a Windows tool used to perform several administrative and automation operations such as managing File Manager operations. More specifically, it supports the “Out-File” cmdlet, which is used to export or redirect the output to a text file. This exported output can be used later. Moreover, it also helps in appending the text to an existing text file. This command was designed to overtake or replace the standard redirect operator “>”.

This write-up will observe in-depth detail to send an output to a file.

How to Send/Redirect Output to a Text File Using PowerShell “Out-File” Cmdlet?

The output can be redirected to a text file by utilizing the “Out-File” command. For that reason:

  • First, add the string or command whose output you want to export to a text file.
  • After that, add a pipeline “|” to transfer the output to an “Out-File” cmdlet.
  • Then, specify the “Out-File” cmdlet and finally add the target file path.

Example 1: Get Date and Time and Redirect to a File Using the “Out-File” Cmdlet

In the below example, first, we have added the “Get-Date” cmdlet to get the date and time. After that, we added the pipeline “|” to transfer the output of the “Get-Date” cmdlet into the “Out-File” command. Then we assigned the file path to the “Out-File” command:

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

Execute the “Get-Content” cmdlet along with the file path to verify the output was redirected to a file or not:

> Get-Content C:\Doc\File.txt

It can be observed that the file contains the current date and time:

Example 2: Redirect a String Output to a File Using “Out-File” Cmdlet

In this below example, first, we have added a text string inside and used the pipeline “|” and the “Out-File” command for redirecting it to the specified file:

> "Hello world" | out-file C:\Doc\File.txt

Execute the given command to verify whether the output was exported or not:

> Get-Content C:\Doc\File.txt

Example 3: Redirect a String to a Text File and Append it

In order to append the text into an existing file just add the “-Append” parameter at the end of the command line:

> "Hi People" | out-file C:\Doc\File.txt -Append

For the verification, execute the given command:

> Get-Content C:\Doc\File.txt

That was all about utilizing Out-Cmdlet command for redirecting output to a file.

Conclusion

The output in PowerShell can be redirected to a file using the “Out-File” cmdlet. For that reason, first, write the string or command whose output you want to send into a file. Then, add the pipeline “|”, and “Out-File” cmdlets, and assign the target file path. This write-up has discussed the approach to send an output to a text file using the “Out-File” cmdlet in PowerShell.

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.