Windows PowerShell


Windows PowerShell Out-File

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 to a file.  In the old days with DOS, 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 Out-File

 ♣

Example 1 To Record Methods and Properties

From the myriad of possible examples, I have selected the ComObject named InternetExplorer as a test-bed for our out-File commands.  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.  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 upshot is that the object's methods and properties are saved into a named file.

Task: To List the InternetExplorer Methods

The scenario: 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.  My advice is to get into the habit of creating cmdlets, thus, save block d) below into a text file with .ps1 extension, for example ie7.ps1.  Then launch PowerShell and navigate to the folder where you saved ie7.ps1.  Now issue the command .\ie7.  Finally open the file created by your cmdlet, iemethod.txt, and compare it with the results below.

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.

PowerShell cmdlet demonstrating out-File
$ie = new-object -com internetExplorer.application
$ie | get-Member -MemberType method |out-File iemethod.txt

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 0:  If you wanted the script to actually open the internet explorer, append this command:
$ie.visible = $true

Note 1:  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 2: 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 3: 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.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Example 2 out-File to Record Services Running

Here is a simple example of employing out-File to record the services actually running on a computer.

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

Final Stage out-File

# PowerShell cmdlet to save service to a file
$service = get-service | where {$_.status -eq "Running" }
$service | format-Table name, status -autosize | out-File servicesRunning.txt

Note:  Try this alternative to {$_.status -eq "Running" } --> {$_.status -eq "Stopped" }

Out-Printer

There is also 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

Summary of Windows PowerShell out-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.

See more Microsoft PowerShell tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.