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:
Execute the “Get-Content” cmdlet along with the file path to verify the output was redirected to a file or not:
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:
Execute the given command to verify whether the output was exported or not:
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:
For the verification, execute the given command:
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.