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
♣
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.
# 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)
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
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
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
|