HOWTO · PowerShell
How to Export Output to Text File in PowerShell
This article shows ways to export the PowerShell output of a common command to a text file.
On this page
To save a command’s displayed output in a text file, pipe it to Out-File and provide a path, as in Get-Process | Out-File -FilePath .\processes.txt. PowerShell creates the file when needed and replaces its contents when that path already exists. Use -Append when you need to keep the existing text. The examples below show how to save selected output, read it back, and choose between a text file and a session transcript.
Export PowerShell output to a text file with Out-File
Out-File receives objects from the pipeline and writes their formatted representation to a file. This makes it useful when the goal is a readable text report. The example compares two in-memory lists, saves the differences, and reads the resulting file; it does not depend on separate input files.
$reference = @('Alpha', 'Beta', 'Gamma')
$difference = @('Alpha', 'Beta', 'Delta')
$outputPath = Join-Path ([System.IO.Path]::GetTempPath()) 'compare-result.txt'
Compare-Object -ReferenceObject $reference -DifferenceObject $difference |
Out-File -FilePath $outputPath
Get-Content -Path $outputPath
Compare-Object compares the two input collections. Its SideIndicator property marks which collection contains each differing item: <= identifies an item from the reference collection, and => identifies one from the difference collection. Here, Gamma and Delta are the differing values. Get-Content displays the text saved at the temporary path; change $outputPath to a full path such as C:\Reports\compare-result.txt to choose a persistent location. This example is not accompanied by an observed output listing, so run it in your PowerShell version to see its formatted table.
Out-File writes formatted text rather than preserving pipeline objects. Its default encoding varies between PowerShell versions. If another program must read the file, specify an encoding supported by the PowerShell version and expected by that program with -Encoding.
Append output to a text file
By default, writing to a path that already exists replaces its contents. Add -Append to retain the current text and add the new command output at the end. For example, Get-Date | Out-File -FilePath .\log.txt -Append appends a date line to log.txt; omit -Append when you intend to create a fresh report. If several commands contribute to one file, use the same encoding for each write so the resulting text stays consistent.
Choose between Out-File and a PowerShell transcript
Use Out-File when you want one command’s output in a known file, or when a script should produce a focused text report. It can replace a report on each run or add to one with -Append. Because it writes formatted text, choose a different export command when the next step needs structured data rather than a human-readable listing.
Use Start-Transcript and Stop-Transcript when you need a record of a broader interactive session, including commands and their displayed output. Start the transcript before the activity you want to record and stop it afterward; choose its path with Start-Transcript -Path. A transcript can include more session detail than a selected command’s output, so use Out-File for a focused result and a transcript for session documentation.