How to send PowerShell output to a text file

Last updated on 07th September 2022

The output of a PowerShell script or Cmdlet is displayed on the console by default. But there are several ways you can redirect this output to a file. You might want to do this if you want to save the results for later use or to process the results by another program or application. This article explains a few different ways to redirect the output of a PowerShell command to a file.

Redirect using Out-File cmdlet

The simplest and probably the easiest way to redirect the output of the PowerShell command is using the Out-File cmdlet. The file output is formatted in the same way as it would have appeared in the console. Here is an example:

Get-ComputerInfo | Out-File -FilePath info.txt

The above command will send the output of Get-ComputerInfo Cmdlet to a file named info.txt in the current directory. If the file already exists then it is overwritten otherwise a new file is created.

The following parameters can be used ith the Out-File cmdlet.

ParameterDescription
-FilePath

Specifies the path of the file to which the output is written. This parameter cannot be omitted. The following command writes the output of Get-ComputerInfo cmdlet to a file name myfile.txt in the C: drive.

Get-ComputerInfo | Out-File -FilePath c:\myfile.txt
-Append

Adds the output to the end of a file instead of overwriting. The following command appends the output of Get-Process to myfile.txt

Get-Process | Out-File -FilePath c:\myfile.txt
-Force

This parameter can be used to overwrite a readonly file.

Get-Process | Out-File -FilePath readonlyfile.name -Force
-Width

To specify the number of characters in each line of output. Any additional character in a line are truncated. Default width is 80 characters. The following command sets the width of the line to 120 characters.

Get-Process | Out-File -FilePath file.txt -Width 120
-Encoding

Specifies the type of encoding for the file. Valid values are ascii, utf7, utf8, utf8BOM, utf8NoBOM, utf32, bigendianunicode, bigendianutf32, oem, unicode.

To set encoding to ASCII,

Get-Process | Out-File -FilePath file.name -Encoding ascii
-NoClobber

Use this parameter to prevent the file being overwritten if it already exist. The following command will show "file already exists" message if the file is present.

Get-ComputerInfo | Out-File -FilePath c:\myfile.txt -NoClobber

Redirect using redirect operators

You can use the PowerShell redirect operators > and >> to redirect the output to a text file. This is same as using the Out-File without parameters.

OperatorDescription
>

Sends the output to a new file or overwrites the if the file already exists. This is same as Out-File without any parameters. Example:

Get-ComputerInfo > info.txt
>>

Appends the output to the file. Same as Out-File -Append

Get-ComputerInfo >> info.txt 

Redirect using Set-Content and Add-Content cmdlets

Set-Content cmdlets writes the output to a file while replacing the existing contents. The file specified with the Path attribute does not exist then it is created. If the file exists then it is overwritten.

If you pass an object to this cmdlet then that object is converted to string using its ToString() method before outputting to file. In some cases it is better to convert the output to string using the Out-String cmdlet before passing it to Set-Content.

Get-ComputerInfo | Out-String | Set-Content -Path info.txt

Add-Content cmdlet is similar to Set-Content except that it appends the output to a file instead of overwriting the existing content.

The use-case for each of these methods are different. If you are running a script or command from console then Out-File is the best choice. If you do not require any parameters with Out-File then use redirect operators. Finally if you writing the output programatically from a script then Set-Content and Add-Content cmdlets are ideal.


Post a comment

Comments

Nothing yet..be the first to share wisdom.