Windows PowerShell Out-File Cmdlet

Introduction to Windows PowerShell Out-File

There are many occasions when you need to output the results of a script not to the screen, but want PowerShell to write to a file.  In the old DOS days we could use the greater than symbol: ‘>’.   However, to redirect PowerShell output we need to master this construction:

Any PowerShell command | Out-File textname.txt  (Note the pipeline (|) symbol.)

Topics for PowerShell Write to File

 ♣

Example 1: Out-File to Record Services Running

Here is a simple example of employing Out-File to write a list of services actually running on a computer to a named file.

Preliminary Stage: Display Running services
Get-Service | where {$_.status -eq "Running" } | ft name, status -autosize.

Final Stage Out-File

# PowerShell write to file.  A list of running services.
$service = Get-Service | where {$_.status -eq "Running" }
$service | ft name, status -auto | Out-File ServicesRun.txt

Note1: To draw attention to the location of where the file gets created you could consider introducing a variable, which can hold the path to the filename that Out-File is looking for.

# Out-File write to file with variable.  Change its value!
$File ="C:\Users\ServicesRun.txt
$service = Get-Service | where {$_.status -eq "Running" }
$service | Format-Table name, status -auto | Out-File $File

Note 2: Try this alternative instead of {$_.status -eq "Running" }
          Substitute –> {$_.status -eq "Stopped" }

PowerShell Out-File -Append

Out-File can be vicious in that overwrites the file.  Fortunately, Out-File has the -append parameter so that you can just add the data to the end of existing entries.

# PowerShell Out-File -Append
$File ="C:\Users\ServicesRun.txt
Get-Date | Out-File $File
Get-Service | Out-File $File -append

Note 3: You could add another -append to the end of the Get-Date line.  See more about PowerShell Get-Date

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Out-File Example 2:  To Record Methods and Properties

Think of Out-File as an appendage to the main PowerShell script.  From the myriad of possible Out-File examples, I have selected the ComObject named InternetExplorer as a test-bed. 

Let us consider the following situation, you employed | Get-Member to review an object’s methods and properties, but now you wish to record the output in a file.  Here is a classic job for the Out-File construction. 

See more about PowerShell Parameters

Task: To List the InternetExplorer Methods

The real-life scenario to append Out-File: We want to employ PowerShell for scripting the InternetExplorer application, but we are not sure which methods are available (and how to spell them).

Instructions

Naturally, you need to install PowerShell as a pre-requisite.  If you have PowerShell v 2.0 then the ISE makes it easy to modify and save the script, for example ie8.ps1. 

# PowerShell cmdlet demonstrating Out-File
$ie = new-object -Com internetExplorer.application
$ie | Get-Member -MemberType method | Out-File iemethod.txt

To draw attention to the place where the file gets created you could consider introducing a variable to hold the path to the filename that Out-File is looking for.

# Out-File Consider changing the value of $File
$File = "C:\Users\IeMethod.txt"
$ie = new-object -Com internetExplorer.application
$ie | Get-Member -MemberType method | Out-File $File

The result: A list of InternetExplorer’s methods, as observed by opening iemethod.txt

Name MemberType Definition
—- ———- ———-
ClientToWindow Method void ClientToWindow (int, int)
ExecWB           Method void ExecWB (OLECMDID, OLECMDEXECOPT,Variant)
GetProperty      Method Variant GetProperty (string)
GoBack            Method void GoBack ()
GoForward       Method void GoForward ()
GoHome           Method void GoHome ()
GoSearch        Method void GoSearch ()
Navigate         Method void Navigate (string,Variant,Variant)
Navigate2       Method void Navigate2 (Variant,Variant,Variant)
PutProperty     Method void PutProperty (string, Variant)
QueryStatusWB Method OLECMDF QueryStatusWB (OLECMDID)
Quit               Method void Quit ()
Refresh          Method void Refresh ()
Refresh2         Method void Refresh2 (Variant)
ShowBrowserBar Method void ShowBrowserBar (Variant,Variant)
Stop               Method void Stop ()

Learning Points

®

Note 4: If you wanted the script to actually open the internet explorer, append this command:
$ie.visible = $true

Note 5:  As ever, Microsoft in general and PowerShell in particular, provide multiple way of achieving the same objective.  For instance you can use -ComObject instead of the depreciated -Com. 

Note 6: Talking of shortened commands, you could dispense with creating a variable and just use:
new-object -Com internetExplorer.application | Get-Member -MemberType method | Out-File iemethod2.txt

Note 7: If you are new to PowerShell, then the pipe (|) instruction enables you to filter the output of the first command.  In this example we issued two | ‘pipe’ instructions, one to get the properties, and one to redirect the output to a file.

Building Up The Script More Slowly
a) Create the InternetExplorer object.
$ie = new-object -Com internetExplorer.application

b) Add the |Get-Member instruction.
$ie = new-object -Com internetExplorer.application
$ie | Get-Member

c) Filter for just the Methods -MemberType -method.
$ie = new-object -Com internetExplorer.application
$ie | Get-Member -MemberType method

d) Finally, append the crucial section: | Out-File iemethod.txt.

See also Tee-Object redirection ยป

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Troubleshooting Out-File

When Out-File does not work as expected, the two most common faults are:

a) Forgetting the pipe (|) before Out-File.

b) Not being able to locate the output file.  For this problem consider preceding the filename with the full path including the drive letter and folder.

Researching Similar Cmdlets – PowerShell’s Out-Printer

# PowerShell Out Cmdlet Research
Clear-Host
Get-Command -verb Out

This reveals 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 any application would.

You could experiment by substituting out-Printer for any of the above Out-File commands  See more on PowerShell printers.

PowerShell Out-Grid

If you have PowerShell v 2.0, see here for brother command Out-Gridview

Summary of PowerShell Write to File

If you need a permanent record of your script’s output, then the key command is: | Out-File filename.txt. Another possible use of this Out-File command is for keeping a list of an object’s methods and properties. My advice is to save the instructions, including | Out-File, into a cmdlet, which you then reuse. 

Note in passing the use of the pipeline (|) symbol.  As usual, its job is to take the output of the left hand expression and make it the input of the right hand expression, in this instance the object’s methods and properties are saved into a named file.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.