How to Save Output to File in PowerShell?

Do you want to save the output of your commands and scripts to a file for later review, logging, or processing in PowerShell? In this tutorial, I will show you several methods to save output to file in PowerShell with examples.

To save output to a file in PowerShell, you can use the Out-File cmdlet, redirection operators, or cmdlets like Set-Content and Add-Content. For example, Get-Process | Out-File -FilePath C:\output\processes.txt will save the list of processes to a text file. For CSV or JSON formats, use Export-Csv or combine ConvertTo-Json with Out-File, respectively. These methods provide efficient ways to capture and store command outputs for later use or analysis.

Save Output to File in PowerShell

Now, let us discuss various methods to save output to a file in PowerShell.

1. Using the Out-File Cmdlet

One of the simplest and most common ways to save output in PowerShell is by using the Out-File cmdlet. This cmdlet sends output to a file, and you can specify the file path and other parameters like encoding and width.

Example:

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

This command gets a list of running processes and saves it to processes.txt in the C:\Bijay directory. You can use the -Append parameter to add the output to the end of an existing file without overwriting it.

Here is the complete PowerShell script:

# Save a list of services to a file
Get-Service | Out-File -FilePath C:\Bijay\services.txt -Encoding UTF8

This script saves the list of services with UTF8 encoding to the services.txt file.

You can see the output in the screenshot below after executing the script using VS Code.

How to Save Output to File in PowerShell

2. Using Redirection Operators

Another method to save output to a file in PowerShell is by using redirection operators. The > operator in PowerShell redirects output to a file, overwriting the file if it exists. The >> operator appends the output to the end of the file in PowerShell.

Example:

Get-Process > C:\Bijay\processes.txt

This command is equivalent to the Out-File example but uses the redirection operator to save the output.

Here is the complete PowerShell script.

# Save disk space information to a file
Get-PSDrive C | Select-Object Used, Free >> C:\Bijay\disk_space.txt

This PowerShell script appends the used and free space information of the C drive to the disk_space.txt file.

3. Using The Set-Content and Add-Content Cmdlets

The Set-Content cmdlet in PowerShell is similar to Out-File, but it is more efficient when writing string data. Add-Content is the counterpart for appending data to a file.

Example:

Get-EventLog -LogName System | Set-Content -Path C:\Bijay\system_log.txt

This command writes the system event log to a file.

Here is the complete script:

# Save custom object to a file
$customObject = [PSCustomObject]@{
    Date = Get-Date
    Files = Get-ChildItem -Path C:\
}
$customObject | Add-Content -Path C:\Bijay\custom_data.txt

This script creates a custom object with the current date and a list of files in the C: root, then appends it to custom_data.txt.

4. Using Start-Transcript and Stop-Transcript

The Start-Transcript cmdlet in PowerShell starts a transcript of all interactions with the PowerShell console, including input and output, and saves it to a file. Stop-Transcript ends the transcription.

Example:

Start-Transcript -Path C:\Bijay\session_transcript.txt
# Run various commands
Stop-Transcript

This example starts a transcript, runs various commands (not shown), and then stops the transcript, saving everything to session_transcript.txt.

Here is the full PowerShell script:

# Transcript a session with multiple commands
Start-Transcript -Path C:\Bijay\session_transcript.txt
Get-Process
Get-Service
Stop-Transcript

This script records a session where the list of processes and services are retrieved, and the transcript is saved to session_transcript.txt.

5. Using Export-Csv and ConvertTo-Csv

You may want to save your output in CSV format when dealing with structured data in PowerShell. The Export-Csv cmdlet and ConvertTo-Csv cmdlet to save the output in a CSV file in PowerShell.

Example:

Get-Process | Export-Csv -Path C:\Bijay\processes.csv

This command exports the list of processes to a CSV file.

Here is the complete example.

# Save system information to a CSV file
Get-ComputerInfo | Select-Object OsName, OsVersion | Export-Csv -Path C:\Bijay\system_info.csv -NoTypeInformation

This script gets the OS name and version from the system and exports it to system_info.csv, excluding the type information that Export-Csv usually includes.

6. Using ConvertTo-Json and Out-File

For a modern data exchange format like JSON, you can use the ConvertTo-Json cmdlet in combination with Out-File in PowerShell.

Example:

Get-Process | ConvertTo-Json | Out-File -FilePath C:\Bijay\processes.json

This command converts the process list to JSON format and saves it to processes.json.

Here is the complete PowerShell script.

# Save services with their status to a JSON file
Get-Service | Select-Object Name, Status | ConvertTo-Json | Set-Content -Path C:\Bijay\services.json

This script selects the name and status of services, converts them to JSON, and saves the output to services.json.

Conclusion

The easiest way to save output to a file in PowerShell is by using the Out-File Cmdlet.

In this PowerShell tutorial, I have explained how to save output to a file in PowerShell using different methods with examples.

  1. Using the Out-File Cmdlet
  2. Using Redirection Operators
  3. Using The Set-Content and Add-Content Cmdlets
  4. Using Start-Transcript and Stop-Transcript
  5. Using Export-Csv and ConvertTo-Csv
  6. Using ConvertTo-Json and Out-File

You may also like: