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.)
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.
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.
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 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!
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" }
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
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.
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.
Guy
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.