Redirect PowerShell Output to a File with Out-File: A Complete Guide

Published:16 June 2019 - 2 min. read

If you’re in the market for a PowerShell cmdlet that can store the output in a text file, you’re in luck. You’re about to learn how to use the PowerShell Out-File cmdlet along with its popular Out-File -Append parameter too!

How Does Out-File Work?

This PowerShell cmdlet is simple in nature; it’s sole purpose is to store output received and store it in a text file.

It was designed to replace the standard output redirection operator (>). Even from the DOS days, we could redirect output to a file. This cmdlet is the PowerShell way to do it.

The Out-File cmdlet is typically used at the end of the PowerShell pipeline. Its sole purpose is to send the raw output directly to a text file with no regard. There are minimal ways to tweak how that output is written.

Sending Output to a File

Let’s say you have a script that returns a list of all of the Windows services on your computer. When you run Get-Service, you will receive all of the objects you might expect on the console.

But maybe you’d like to save that output to a text file. The Out-File cmdlet is a great way to do that. You can use it by piping just about anything to to it via the pipeline.

You can see below, the output of Get-Service was sent to the cmdlet which created a text file called Services.txt that contains the exact display that you see on the console.

PS> Get-Service | Out-File -FilePath C:\Services.txt
PS> Get-Content C:\Services.txt
Status   Name               DisplayName

Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Stopped  Appinfo            Application Information
Stopped  AppMgmt            Application Management
<SNIP>

Appending to a File

By default, it overwrites anything in the text file provided via the FilePath parameter. However, you can override this behavior by using the Append parameter.

Perhaps you’re accumulating console output in a file and would like to append text to a file rather than overwriting. The Append parameter is your best friend.

By using the Out-File -Append parameter you can see below, notice that if you don’t use it the C:\File.txt is overwritten. But, as soon as you tack on the Append parameter, it appends the output to the end.

PS> 'foo' | Out-File -FilePath C:\File.txt
PS> Get-Content -Path C:\File.txt
foo
PS> 'foo' | Out-File -FilePath C:\File.txt
PS> Get-Content C:\File.txt
foo
PS> 'foo' | Out-File -FilePath C:\File.txt -Append
PS> Get-Content C:\File.txt
foo foo

Changing Up Output

By default, this cmdlet will attempt to replicate what’s shown on the console but there are some ways to manipulate it. For example, the cmdlet has a NoNewLine parameter that merely removes all newline characters.

PS> Get-Service | Out-File -FilePath C:\Services.txt -NoNewline
PS> Get-Content C:\Services.txt
Status   Name               DisplayName
------   ----               -----------
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application La

Or, you can cut off the text on each row at a particular character count as well with the Width parameter.

PS> Get-Service | Out-File -FilePath C:\Services.txt -Width 30 
PS> Get-Content C:\Services.txt
Status   Name               DisplayName
------   -----              -----------
Stopped  AJRouter           Al

Conclusion

out-file
Out-File

In a real-world scenario, this cmdlet works but there are better ways to store output to a file.

This cmdlet provides no real options to manipulate the output. It simply saves the output exactly as shown in the console. This cmdlet is similar to a PowerShell transcript that just writes everything directly to a text file.

The Add-Content or Set-Content cmdlets will always have more flexibility but if you need a quick way to get the PowerShell output to a file, Out-File is the way to go.

Related: Set-Content: The PowerShell Way to Write to a File

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!