Using the Out-File Cmdlet

Saving Data Directly to a Text File

The Out-File cmdlet enables you to send pipelined output directly to a text file rather than displaying that output on screen. For example, this command retrieves process information using Get-Process, then uses Out-File to write the data to the file C:\Scripts\Test.txt:

Get-Process | Out-File c:\scripts\test.txt

By default Out-File saves the data exactly the way that data appears in your Windows PowerShell console. That means that some of the data could end up truncated. For example, take a look at the results of running Get-Process in the console:

If you look closely, you’ll notice that the name of at least one process (PhotoshopElementsF …) could not be fully displayed onscreen. In turn, that’s exactly how this line will look in the saved text file:

61       5     1240        660    30     0.05   1084 PhotoshopElementsF...

That’s a problem, but it’s an easy problem to solve: just include the -width parameter and specify a different line width (in characters) for the text file. For example, this revised command sets the line width to 120 characters:

Get-Process | Out-File c:\scripts\test.txt -width 120

And here’s what that same line looks like in the new and improved text file:

61       5     1240        660    30     0.05   1084 PhotoshopElementsFileAgent