Viewing a script's output on screen is all well and good, but often it's more convenient to save PowerShell's results to a file. Enter
PowerShell's command: out-File. Here we have an instruction that you simply bolt-on to an
existing script. ..... | out-File "Filename.txt". Incidentally, the simplicity of out-File is one killer reason to use PowerShell instead of VBScript.
Appending out-File to an existing command is easy. The biggest danger is 'over-think'; just remember that PowerShell takes
care of opening and closing the file automatically. Consequently, there is no need to waste time looking for non-existent open-file, or save-file commands. If the file specified by out-File does
not already exist, PowerShell even creates it for you.
This is how the command works. Assuming the first part of the script
delivers the results, redirect the output into a file with a command such as: | out-File c:\ logs\result1.txt.
This example is purely to concentrate on the out-File command. In fact, the sooner we move on to example 2, the sooner we can do some real work.
# PowerShell script to list the files in C:\Windows\System32 Get-ChildItem "C:\Windows\System32" | out-File "D:\Files\Sys32.txt"
Note 1: While out-File creates the file, you have to make sure that the path exists because out-File cannot create folders. In this instance, the alternative is to adjust D: \files to C: \PS, or an existing folder on your machine.
Once I get a command to work - and I like it, I want to know more. Get-Help always reveals at least one
parameter that I had taken for granted, forgotten, or previously overlooked.
Get-Help out-file -full (help out-File -full) also works. Be aware that there is no
need for a pipe (|) with help.
If you
append the -full switch, then PowerShell's help reveals useful parameters, for example, -filepath (taken for granted) -append (forgotten) -NoClobber (previously overlooked).
Guy Recommends: SolarWinds Engineer's Toolset v10
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates.
There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10
This
example has extra features, which make the script more useful. Thanks to the -recurse parameter, the script is designed to drill down into the subdirectories. The question mark '?' introduces
a 'Where' statement, its purpose is to filter just the dll files. Also, the output contains only the two file properties that we are interested in: name and creationtime.
# PowerShell script to list the dll files under C:\Windows\System32 $DllFiles = gci "C:\Windows\System32" -recurse | ? {$_.extension -eq ".dll"} ` |Format-Table name, creationtime -auto |
out-File
-filepath "D:\files\dll.txt"
Note 1: Spot the tiny backtick symbol ` at the end of line 2.
This plain backtick tells PowerShell that the command continues on the next line.
Note 2: The
parameter -filepath is optional. If you omit -filepath, PowerShell's intelligence deduces from the sequence of commands that "D:\ps\files\dll.txt" is the place to save the file. If there are
no spaces in the path, then you can even omit the speech marks.
Note 3: If you get an error message 'Cannot find part of the path', then amend D: \files to a real folder on your
machine.
If you run the script for a second time, have you noticed how out-File
over-writes information? If your intention was to add data at the end of the file, then
one solution would be to replace out-File with add-Content. However, because of the superior formatting, I prefer to stick with
out-File and add the -append parameter.
-Append
| Out-File -filepath "D:\Files\dll.txt" -append
-NoClobber
What can 'No Clobber' mean? Lost your clothes? Not so, NoClobber means don't over-write the file. It seems to me that you would incorporate the -noclobber
in circumstances where your intention was to save lots of files with slightly different names, and you did not want to risk losing their contents by over-writing.
Note 1: If you insist on running the script again, the -noclobber parameter causes PowerShell to generate an error message and thus protects the original file from being replaced.
Content Family
Out-File is an honorary member of the 'Content' family, here are the cousin commands. I don't
often use these other commands because they don't format the data as I would like.
Out-File has 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 you would print from any other application.
You could experiment by substituting out-Printer for any of the above out-File commands
Export-Csv also Import-Csv
Spreadsheets are a convenient vehicle for importing and exporting objects
into Active Directory. Begin by appending
export-Csv to QAD scripts which
list users or computers.
Summary of PowerShell's Out-File
Trust me, you will make great use of PowerShell's out-File for saving results to a text file. This is a straight-forward command to bolt-on to existing commands. The main danger for newbies
is looking for non-existent commands; remember that PowerShell takes care of both file open and file close.
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.