Prof. Powershell

Professor Powershell: On File

Powershell includes a cmdlet called Out-Files that makes saving command results to a text file easier.

A common administrative task is to save the results of a command or query to a text file. In the CMD shell, the traditional method is to use the console redirection characters > and >>. You can still use these operators in PowerShell:

PS C:\> get-process > processes.txt

There's nothing wrong with this technique in PowerShell. I've yet to find a situation in which it doesn't work.

But PowerShell has a cmdlet called Out-File, which offers a bit more flexibility. This cmdlet should be at the end of your pipelined expression:

PS C:\> get-process | 
where {$_.workingset -gt 10mb} | 
out-file LargeProc.txt

Used without any parameters, Out-File will overwrite any existing file with the same name. The cmdlet includes an -append parameter which will give you the same result as if you had used the >> redirection operators.

However, the cmdlet also has a nice feature to keep you from accidentally overwriting a file. You can use the -NoClobber parameter:

PS C:\> get-process | where 
{$_.workingset -gt 10mb} | out-file 
LargeProc.txt -noclobber

When you use this parameter, if the file already exists PowerShell will display an error message:

Out-File : File C:\LargeProc.txt already exists 
and NoClobber was specified.

The cmdlet also supports the -whatIf and -Confirm parameters. These are also great sanity checks.

By the way, you don't have to rely on the pipeline to use Out-File. You can use its -InputObject parameter, which will take the specified object and send it to a file:

PS C:\> $p = get-process
PS C:\> out-file myprocs.txt -input $p

Finally, depending on your situation or the expression, Out-File supports several encoding schemes. Use the -Encoding parameter. The default is Unicode, but you can choose from ASCII, OEM, Default, BigEndianUnicode, UTF7, UTF8 and UTF32.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Mon, Oct 13, 2008 Anonymous Anonymous


Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.