Ezine 154 – PowerShell’s Out-File

Ezine 154 – PowerShell’s Out-File

Out-File is a classic PowerShell technique to redirect your script’s output to a text file.

Out-File Topics


This Week’s Secret

Out-File is a mixture of the icing on the cake, and the killer reason to switch from VBScript to PowerShell.  Imagine this situation, you have created a beautiful VBScript which employs WMI to interrogate the eventlog, the disk, or some other aspect of the operating system.  However, to complete the job you need to go the extra mile and store the resulting information in a file.  With VBScript you are now faced with a major series of tasks, you need to create the FileSystemObject, make a new file, open it, then transfer the crucial WMI information.

Contrast with PowerShell, where all you need is to append this simple command:
out-File filename.  Where filename is the path to the place where you store the script’s output.

This Week’s Mission

Appending out-File to an existing command is easy.  The biggest danger is ‘over-think’; just remember that PowerShell takes care of opening and closing the file automatically.  Consequently, there is no need to waste time looking for non-existent open-file, or save-file commands.  If the file specified by out-File does not already exist, PowerShell even creates it for you.

This is how the command works.  Assuming the first part of the script delivers the results, redirect the output into a file with a command such as: | out-File c:\ logs\result1.txt.

Example 1 – Starting with Out-File

This example is purely to concentrate on the out-File command.  In fact, the sooner we move on to example 2, the sooner we can do some real work.

# PowerShell script to list the files in C:\Windows\System32
Get-ChildItem "C:\Windows\System32" | out-File "D:\Files\Sys32.txt"

Note 1:  While out-File creates the file, you have to make sure that the path exists because out-File cannot create folders.  In this instance, the alternative is to adjust D: \files to C: \PS, or an existing folder on your machine.

Research Out-File with Get-Help

Once I get a command to work – and I like it, I want to know more.  Get-Help always reveals at least one parameter that I had taken for granted, forgotten, or previously overlooked.

Get-Help out-file -full
(help out-File -full) also works.  Be aware that there is no need for a pipe (|) with help.

If you append the -full switch, then PowerShell’s help reveals useful parameters, for example, -filepath (taken for granted) -append (forgotten) -NoClobber (previously overlooked).

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 2 – Putting Out-File to Work

This example has extra features, which make the script more useful.  Thanks to the -recurse parameter, the script is designed to drill down into the subdirectories.  The question mark ‘?’ introduces a ‘Where’ statement, its purpose is to filter just the dll files.  Also, the output contains only the two file properties that we are interested in: name and creationtime.

# PowerShell script to list the dll files under C:\Windows\System32
$DllFiles = gci "C:\Windows\System32" -recurse | ? {$_.extension -eq ".dll"} `
|Format-Table name, creationtime -auto | out-File -filepath "D:\files\dll.txt"

Note 1:   Spot the tiny backtick symbol ` at the end of line 2.  This plain backtick tells PowerShell that the command continues on the next line.

Note 2:  The parameter -filepath is optional.  If you omit -filepath, PowerShell’s intelligence deduces from the sequence of commands that "D:\ps\files\dll.txt" is the place to save the file.  If there are no spaces in the path, then you can even omit the speech marks.

Note 3:  If you get an error message ‘Cannot find part of the path’, then amend D: \files to a real folder on your machine.

Example 3 – More parameters (-Append and -NoClobber)

If you run the script for a second time, have you noticed how out-File over-writes information?  If your intention was to add data at the end of the file, then one solution would be to replace out-File with add-Content.   However, because of the superior formatting, I prefer to stick with out-File and add the -append parameter.


| Out-File -filepath "D:\Files\dll.txt"  -append


What can ‘No Clobber’ mean?  Lost your clothes?  Not so, NoClobber means don’t over-write the file.  It seems to me that you would incorporate the -noclobber in circumstances where your intention was to save lots of files with slightly different names, and you did not want to risk losing their contents by over-writing.

| Out-File -filepath "D:\Files\dll June.txt"  -NoClobber

Note 1:  If you insist on running the script again, the -noclobber parameter causes PowerShell to generate an error message and thus protects the original file from being replaced.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Content Family

Out-File is an honorary member of the ‘Content’ family, here are the cousin commands.  I don’t often use these other commands because they don’t format the data as I would like.

Add-Content (Appends)
Set-Content (Replaces existing content)


Out-File has a sister command called out-Printer.  If you try:
Help out-Printer

Then you can see that you don’t need to specify the name of the printer, PowerShell automatically selects the default printer, just as you would print from any other application.

You could experiment by substituting out-Printer for any of the above out-File commands

Summary of PowerShell’s Out-File

Trust me, you will make great use of PowerShell’s out-File for saving results to a text file.  This is a straight-forward command to bolt-on to existing commands.  The main danger for newbies is looking for non-existent commands; remember that PowerShell takes care of both file open and file close.

See more about out-File and other PowerShell scripts here