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
