Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Controlling PowerShell's Results with Out-File

PowerShell Write To Text File - Out-File

Viewing a script's output on screen is all well and good, but often it's more convenient tell PowerShell to write text file.  This a task for 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.

Topics for PowerShell Output to File

 ♣

Introduction to Out-File

Writing the results of a command into a file is easy with PowerShell's Out-File.  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 PowerShell output to a file with a command such as:
PowerShell-Command | out-File c:\ logs\result1.txt.

Example 1 - Starting with Out-File

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 write to text file
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.

Research Out-File With Get-Help

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, so it is with PowerShell's write to text file.

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

See more examples of PowerShell's output to file.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

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.

Download your free copy of WMI Monitor

Example 2 - Putting Out-File to Work

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"
$DllFiles

Note 2:   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 3:  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 4:  If you get an error message 'Cannot find part of the path', then amend D: \files to a real folder on your machine.

®

Example 3 - More Parameters (-Append and -NoClobber)

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.

PowerShell Out-File -Append

Out-File can be vicious in that it 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:\PowerShell\Processes.txt"
Get-Date | Out-File $File
Get-Process | Out-File $File -append

Note 5: You could add another -append to the end of the Get-Date line.

Another -Append Example

| Out-File -filepath "D:\Files\dll.txt"  -append

PowerShell -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.

| Out-File -filepath "D:\Files\dll June.txt"  -NoClobber

Note 6:  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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

SolarWinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of SolarWinds' offer.

Download a free trial of the Network Performance Monitor.

PowerShell's 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.

Add-Content (Appends)
Clear-Content
Get-Content
Set-Content (Replaces existing content)

Out-Printer

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

Out-Gridview

With PowerShell v 2.0 you can output to a gridview.

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.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

A Different Technique: PowerShell Redirection

In many ways this is the simplest technique yet.  Just append the greater than sign >.  However, the killer reason for redirecting the output of a PowerShell command is when you want to record error messages or warnings.

Get-Service > D:\PShell\Services.txt

Note 7: This is the same as Get-Service | Out-File D:\PShell\Services.txt

The killer reason for using this techique is if you want to redirect warning messages, for instance you want to see if a service jkl exists:

Get-Service jkl 2> D:\PShell\Services.txt

Note 8: The key redirect command is 2>

See more On PowerShell 3.0 Redirection.

Summary of PowerShell's Out-File

Trust me, you will make great use of the ability of PowerShell to write to text file.  Out-File is the perfect command for piping the results to a text file.  This is a straight-forward command to bolt-on to existing cmdlets.  The main danger for newbies is looking for non-existent commands; remember that PowerShell takes care of both opening and closing the named file.

Footnote:
Thanks to Danny Beaman for correcting 3 errors on this page.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell file tutorials:

PowerShell Home   • Add-Content  • Get-Content  • Set-Content  • Test-Path  • -recurse

Files  • PowerShell Registry  • PowerShell ItemProperty  • Compare-object

Get-Credential   • Windows PowerShell

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.

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.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: