Using Out-File Cmdlet to Redirect Output to File in PowerShell

With PowerShell, you can redirect the output of any command to a text file. This is useful if you want to save the results returned by a PowerShell command or script. The PowerShell Out-File cmdlet is used to redirect output to a plain text file.

PowerShell has a class of Out- cmdlets. All these commands are used to redirect the received data to input devices (file, printer, screen). A list of PowerShell Out- cmdlets can be obtained as follows:

Get-Command -Verb Out

powershell redirect output to file

To display information about the syntax of the Out-File cmdlet, use the command:

Get-Help Out-File

powershell redirect all output to file

Out-File [-FilePath] <string> [[-Encoding] {unknown | string | unicode | bigendianunicode | utf8 | utf7 | utf32 | ascii | default | oem}] [-Append] [-Force] [-NoClobber] [-Width <int>] [-NoNewline] [-InputObject <psobject>] [-WhatIf] [-Confirm] [<CommonParameters>]

The Out-File cmdlet allows you to receive an object as input using the pipeline operator (|) and save the received data to a file.

For example, to get a list of running processes and save it to a file, run the command:

Get-Process | Out-File c:\ps\proccess.txt

The path to the text file is specified as a parameter. The cmdlet will create a new proccess.txt file in the specified directory. If the specified directory doesn’t exist, the Out-File cmdlet will return an error:

Out-File : Could not find a part of the path ‘C:\ps\proc.txt’.

To open this text file in Notepad, run the command:

notepad c:\ps\proccess.txt

Hint. You won’t see anything on the screen when running the command because the Out-File cmdlet doesn’t produce any output. It only accepts incoming data and transfers its text interpretation to a file. The pipeline is cleared after it.

windows powershell redirect output to file

You can save the list of running services to a file on your desktop using the Get-Service command:

Get-Service | Where-Object {$_.Status -EQ "Running"}| Format-Table -AutoSize| Out-File -filepath "$Env:userprofile\Desktop\RunningServices.txt

To get the contents of the resulting file and display it in the PowerShell console, use the command:

Get-Content "$Env:userprofile\Desktop\RunningServices.txt"

send output to file powershell

By default, the Out-File cmdlet overwrites data in an existing file. If you want to prevent overwriting the file, use the NoClobber parameter. If you need to add new data to the end of the file, use the Append parameter:

Get-Process | Out-File c:\ps\proccess.txt –Append

You can use aliases instead of the Out-File cmdlet. They are similar to the redirection characters in the Windows Command Prompt (cmd.exe).

READ ALSO  How to Flush DNS Cache in Windows 10

For example, you can send data to a file like this:

Get-Process > c:\ps\proccess.txt

Or append new data to the end of the file:

Get-Process >> c:\ps\proccess.txt

windows powershell redirect output to file

By default, Out-File saves the output to a Unicode encoded file (UCS-2 LE BOM).

You can use other encodings. They can be specified using the Encoding parameter. For example, to save the file in UTF-8:

Get-Process | Out-File c:\ps\proccess.txt -encoding utf8

Or UTF-8-BOM:

Get-Process | Out-File c:\ps\proccess.txt -encoding ascii

Out-File formats the contents of the files to look like the output of the console. As a result, the output is truncated. You can specify the width of the line using the Width parameter (maximum value 2147483647):

Get-Command | Out-File -FilePath c:\ps\wideoutput.txt -Width 1000000
Cyril Kardashevsky

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.