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.txtThis 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 UTF8This 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.

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.txtThis 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.txtThis 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.txtThis 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.txtThis 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-TranscriptThis 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-TranscriptThis 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.csvThis 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 -NoTypeInformationThis 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.jsonThis 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.jsonThis 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.
- Using the Out-File Cmdlet
- Using Redirection Operators
- Using The Set-Content and Add-Content Cmdlets
- Using Start-Transcript and Stop-Transcript
- Using Export-Csv and ConvertTo-Csv
- Using ConvertTo-Json and Out-File
You may also like:
- How To Create File If Not Exists In PowerShell?
- How to Check if a File Exists in PowerShell?
- How To Delete File If Exists In PowerShell?
- How to Replace Text in a File Using PowerShell?
- How to Create a Password-Protected Zip File Using PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.