PowerShell Out-File [With Examples]

Do you want to save the result of a PowerShell commandlet? The Out-File cmdlet enables you to send the output of your PowerShell commands directly to a file.

In this tutorial, I will explain everything about the PowerShell Out-File cmdlet, including its syntax, examples, etc.

What is the Out-File Cmdlet in PowerShell?

Out-File is a built-in PowerShell cmdlet that allows you to send (or “pipe”) the output of commands into a file. This is useful for logging, saving reports, or simply keeping a record of your scripts’ results. Out-File preserves the formatting of the output as it appears in the PowerShell console, making it ideal for human-readable logs.

PowerShell Out-File Syntax

The basic syntax for Out-File is:

Out-File [-FilePath] <string> [-Append] [-Encoding <string>] [-Force] [-Width <int>] [<CommonParameters>]

The most common way to use Out-File is by piping (|) the output of another command into it:

Get-Process | Out-File -FilePath "C:\output.txt"

Key Parameters:

  • -FilePath (or just the path as first argument): The path to the file where output will be saved.
  • -Append: Adds output to the end of the file instead of overwriting it.
  • -Encoding: Sets the file encoding (e.g., UTF8, ASCII).
  • -Force: Creates the file even if the target directory does not exist or if the file is read-only.
  • -Width: Sets the line width for the output (default is 80).

Check out PowerShell Remove-Item Cmdlet

PowerShell Out-File Examples

Here are some examples of using the PowerShell Out-File cmdlet.

Example 1: Basic Usage – Save Output to a File

Let’s start with a simple example. Suppose you want to save a list of running processes to a text file:

Get-Process | Out-File -FilePath "C:\Reports\processes.txt"

This command runs Get-Process, which lists all running processes, and saves the output to processes.txt in the C:\Temp folder. If the file does not exist, PowerShell creates it.

Here is the exact output in the screenshot below:

PowerShell Out-File

Example 2: Appending Output to an Existing File

If you want to add new output to the end of an existing file (instead of overwriting it), use the -Append parameter:

Get-Service | Out-File -FilePath "C:\Reports\output.txt" -Append

This will add the list of services to the end of output.txt, preserving any previous content.

Check out PowerShell Greater Than or Equal

Example 3: Setting the Encoding

By default, Out-File uses Unicode encoding, but you can specify another encoding such as UTF8 or ASCII. This is especially important if you plan to share the file with other systems or applications.

Get-Content "C:\Windows\win.ini" | Out-File -FilePath "C:\Reports\winini.txt" -Encoding UTF8

This command reads the contents of win.ini and saves them as a UTF8-encoded file.

Example 4: Forcing File Creation and Overwriting Read-Only Files

The -Force parameter can be used to overwrite read-only files or create files in directories that require special permissions.

Get-ChildItem C:\ | Out-File -FilePath "C:\Reports\directorylist.txt" -Force

If directorylist.txt is read-only, or if the folder requires special permissions, the -Force option ensures the file is created or overwritten.

Example 5: Customize Output Width

By default, Out-File formats each line to 80 characters. If your output gets cut off or you want wider lines, use the -Width parameter:

Get-EventLog -LogName System -Newest 20 | Out-File -FilePath "C:\Reports\events.txt" -Width 200

This increases the line width to 200 characters, ensuring more data fits on each line.

Conclusion

In this tutorial, I explained how to use the Out-File cmdlet in PowerShell with various real examples. Whether you’re logging, reporting, or simply saving results for later, Out-File makes it easy to capture exactly what you see in the console.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.