Similar to a previous cmdlet of a day we covered, Set-Content, PowerShell also has another cmdlet called Out-File that performs a similar feat which is write output directly to a text file.

If you're a beginner/intermediate PowerShell scripter, be sure to check out my FREE mini-course on Building a PowerShell Tool! 9,000+ words of deep explanation and insights on how to build a PowerShell tool.

Unlike the general Set-Content cmdlet that's purpose is a universal cmdlet to create or overwrite a file at any point in a script, the Out-File PowerShell cmdlet 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.

Basic Out-File Usage

This cmdlet is simple to use as was the standard output redirection operator before it. 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.

To demonstrate, I can retrieve all of the services from a local machine which returns them to the console as you'd expect. If I redirect that output through the PowerShell pipeline to this cmdlet, you can see that same output gets written to a file instead of 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 with Out-File

By default, this cmdlet will overwrite a file. Think Set-Content. However, we can also use the Append parameter if perhaps we have some process that's collecting information as it goes along.

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 Out-File 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, it 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, we can cut off the text on each row at a particular character count as well.

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

Stopped  AJRouter           Al

In my experience, I do not find myself using this cmdlet too often. It gives you no real options to manipulate the output and forces you to massage the output to exactly how it's supposed to look before writing to the file. Out-File is similar to a PowerShell transcript where it writes everything directly to a text file. I will always prefer either Add-Content or Set-Content to write to files but if you need a quick way to get the PowerShell output to a file, Out-File is the way to go.

For a full breakdown, check out the Microsoft documentation.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!