PowerShell Out-File vs Export-Csv vs Redirect: Save Output

PowerShell Out-File vs Export-Csv vs Redirect: Save Output

PowerShell Tips Editor 2 min read
PowerShell Out-File vs Export-Csv vs Redirect: Save Output

Screenshotting a terminal window is not documentation. When a script produces important output, saving it to a file immediately makes it auditable, shareable, and persistent across reboots. PowerShell save output to file has five distinct approaches — redirect with >, append with >>, Out-File with encoding control, Export-Csv for structured data, and Tee-Object to see output and save simultaneously. Each serves a different scenario, and knowing which to choose prevents subtle encoding and data-loss bugs.

Quick Answer / TL;DR

For structured data: Export-Csv -NoTypeInformation. For text output: Out-File -Encoding UTF8. For both console and file: Tee-Object -FilePath.

Redirect with > Operator

The > operator redirects the success output stream to a file, overwriting any existing content. It’s the quickest option for simple output capture.

# Overwrite file with command output
Get-Service > "C:\Logs\services.txt"

# Capture output from a script
.\MyScript.ps1 > "C:\Logs\script-output.txt"

# Redirect error stream too (2>&1 = error stream to success stream)
Get-Process -Name "noexist" 2>&1 > "C:\Logs\errors.txt"

Important: On Windows PowerShell 5.1, > writes UTF-16 LE with BOM by default. This causes issues with tools expecting UTF-8. Use Out-File -Encoding UTF8 for better compatibility.

Append with >> Operator

The >> operator appends to an existing file rather than overwriting it. Same encoding behavior as >.

# Append to a log file with a timestamp
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Script started" >> "C:\Logs\operations.log"
Get-ChildItem "C:\Temp" | Select-Object Name, Length >> "C:\Logs\operations.log"
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Script completed" >> "C:\Logs\operations.log"

Out-File with Encoding Option

Out-File gives you explicit encoding control — essential when the output will be read by tools expecting UTF-8 or when writing batch log files. Use -Append to add to an existing file.

# Write with UTF-8 encoding (no BOM)
Get-Process | Out-File -FilePath "C:\Logs\processes.txt" -Encoding UTF8

# Append to existing file
"New entry: $(Get-Date)" | Out-File -FilePath "C:\Logs\audit.txt" -Encoding UTF8 -Append

# Specify width to prevent truncation of long lines
Get-Service | Format-Table -AutoSize | Out-File -FilePath "C:\Logs\services.txt" `
    -Encoding UTF8 -Width 200

Available encodings: UTF8, UTF8NoBOM (PS6+), Unicode, ASCII, UTF32.

Export-Csv for Structured Data

Export-Csv is specifically for pipeline objects — it creates a properly formatted CSV where each property becomes a column and each object becomes a row. This is the correct method when you plan to open the output in Excel or import it into another system.

# Export process list to CSV
Get-Process | Select-Object Name, Id, CPU, WorkingSet |
    Export-Csv -Path "C:\Logs\processes.csv" -NoTypeInformation

# Export with UTF-8 encoding
Get-Service |
    Export-Csv -Path "C:\Logs\services.csv" -NoTypeInformation -Encoding UTF8

# Append to existing CSV (adds rows, no header on append)
Get-Service | Where-Object Status -eq "Running" |
    Export-Csv -Path "C:\Logs\running-services.csv" -NoTypeInformation -Append

Always use -NoTypeInformation to omit the #TYPE comment line that appears at the top of CSV files by default.

Tee-Object to See and Save

Tee-Object splits the pipeline — output flows to both a file and to the next cmdlet (or the console). Use it when you want real-time visibility into output while simultaneously saving it.

# Show on console AND save to file simultaneously
Get-Process | Tee-Object -FilePath "C:\Logs\processes.txt"

# Tee to a variable and continue pipeline
Get-ChildItem "C:\Logs" -File |
    Tee-Object -Variable files |
    Where-Object Length -gt 1MB |
    Select-Object Name, Length

Write-Output "Total files found: $($files.Count)"

Choosing the Right Method

Summary decision guide:

  • > — Quick one-off captures; be aware of UTF-16 encoding on PS5.1
  • >> — Simple append operations; same encoding caveat
  • Out-File -Encoding UTF8 — Text output when encoding matters; log files for other tools
  • Export-Csv -NoTypeInformation — Structured data, Excel, database import
  • Tee-Object — Both display and save simultaneously

Common Errors and Fixes

  • > uses UTF-16 by default in PS5.1: Files created with > in Windows PowerShell 5.1 are UTF-16 LE with BOM. Linux tools, Python scripts, and many editors handle this fine, but some older utilities cannot. Fix by switching to Out-File -Encoding UTF8 or using PowerShell 7 where > defaults to UTF-8.
  • Out-File converts objects to strings — use Export-Csv for data: Out-File calls ToString() or the default formatter on objects before writing. The result looks like table output — formatted text, not data. If you need the underlying property values in a structured format, use Export-Csv (for CSV) or Export-Clixml (for PowerShell object round-tripping).

Related Cmdlets / See Also

Wrapping Up

For new scripts, default to Export-Csv -NoTypeInformation for structured data and Out-File -Encoding UTF8 for text logs — these choices avoid the encoding surprises that come with the redirect operators. As a next step, add Tee-Object to a long-running script so you get real-time console visibility while the full output is captured to a timestamped log file for later review.

Send-Item -To