Out-FileSimilar to a previous cmdlet of a day we covered, [Set-Content](https://www.adamtheautomator.com/powershell-write-file-set-content/), PowerShell also has another cmdlet called Out-File that performs a similar feat which is write output directly to a text file. 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.

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.

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.

Or, we can cut off the text on each row at a particular character count as well.

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.

%d bloggers like this: