liveBook · Manning

Out-File

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Out-File is a PowerShell cmdlet that writes command output to a file.

Out-File is a PowerShell cmdlet that takes pipeline output and writes it to a flat text file instead of displaying it on the host. Use it when you need to preserve command results, such as a directory listing or formatted process table. section 6.2.4 section 6.3

Use Out-File in these practical ways:

  • Save selected object properties: Get-ChildItem | Select-Object Name | Out-File process.txt. section 6.2.4
  • Use the redirection shortcut Dir > DirectoryList.txt, or write the equivalent explicitly as Dir | Out-File DirectoryList.txt. section 6.3
  • Add formatting parameters such as -Width when an 80-column default would alter a wide table; other parameters support encodings and appending. section 6.3 section 11.7
  • Place Out-File after a Format- cmdlet when saving formatted instructions, and do not send those instructions to another object-processing cmdlet. section 11.9.1

Save a formatted history report

Get-History | Select-Object Id,Duration,CommandLine | Format-Table | ConvertTo-Html | Out-File history.html. Here, Out-File receives the final generated content and writes it to a file. section 11.9.1

Dir | Out-File DirectoryList.txt

Browse the equivalent pipeline form for sending a directory listing to a text file. section 6.3

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains pipeline placement, redirection, formatting flow, 80-column width, and additional parameters without detailing every parameter.
Examples
Uses directory listings, selected child-item names, formatted process output, and an HTML history report.
What you can do after
Operate/configure it to save pipeline output, adjust file width, append content, choose encodings, and preserve formatted reports.
How it connects
Connects Out-File to the PowerShell pipeline, redirection, formatting cmdlets, Out-Default, and Out-Host.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.