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.)
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" }
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
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.
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.
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
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.
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 printer
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
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.