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!
Table of Contents
How Does Out-File Work?
The PowerShell Out-File cmdlet is simple in nature; it’s sole purpose is to store output received and store it in a text file.
Out-File 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.
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
Out-File 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
Out-File overwrites anything in the text file provided via the
FilePath parameter. However, you can override this behavior by using the
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
PS> Get-Service | Out-File -FilePath C:\Services.txt -Width 30 PS> Get-Content C:\Services.txt Status Name DisplayName ------ ----- ----------- Stopped AJRouter Al
In a real-world scenario, the
Out-File cmdlet works but there are better ways to store output to a file.
Out-File 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.
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.
More from Adam The Automator & Friends
Expose REST APIs, schedule jobs, and provide custom user interfaces with the platform built for PowerShell to integrate with anything.
We’ve put together a list of the resources we, at ATA, can wholeheartedly recommend!
Why not write on a platform with an existing audience and share your knowledge with the world?