Ezine 175 – Out-File PowerShell’s Killer Reason to Switch from VBScript

Ezine 175 – PowerShell’s Out-File

Whenever you need to save the results returned by your PowerShell script, all you need to do is append | Out-File -path.

Topics for PowerShell

 ♣

This Week’s Secret

If I could only mention one technique to lure people from VBScript to PowerShell, that technique would be out-File.   All VBScripts tend to be longer than their PowerShell equivalent.  Consequently, by the time a beginner has coded the main purpose of the script, they lose the plot when it comes to adding convoluted commands to create a file, open it, write data to the file, then clear up and close the file.  I say again, PowerShell commands are short and sweet.  Moreover, to save the output to a file just pipe the results with | out-file -path c:\ wherever.txt.

This Week’s Mission

In recent ezines I have neglected to tell you about out-File, not because I don’t like it, more that I wanted to concentrate on the basics of each cmdlet that I have introduced.  I prefer to focus on one topic per ezine, and leave readers with a clear idea of what get-Eventlog, or get-WmiObject does.  For those with intermediate experience I hope that my ezines will become a reference point for when they forget the finer point of a particular command.  Nevertheless, no excuses, I should have mentioned out-File earlier, because sooner or later everyone wants to save the results of the script to a file on disk.

Example 1: Out-File

If the most difficult part of explaining the out-File cmdlet is fine-tuning the path, then you know this is a straightforward technique.  Our only problem is that I save my files to a folder called ‘powershell’ on the E: drive.  I seriously doubt you have such a folder, therefore you must either amend the path (best) or create a folder called powershell on your E: drive.

Instructions:

Pre-requisite: Visit Microsoft’s site and download the correct version of PowerShell for your operating system.

  • Launch PowerShell
  • Copy the six lines of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit –> Paste
  • Press enter to execute the code.

Example 1: Out-File Saves Event Log Entries to Disk

I have chosen get-Eventlog as the vehicle to test out-File, please feel free to use any other scripts if you prefer.

# PowerShell script to demonstrate out-File
# Author: Guy Thomas
# Version 1.0 October 2008 tested on PowerShell v 1.0

$FilePath = "E:\powershell\event50.txt"
get-EventLog system -newest 50 | out-File $FilePath

Learning Points

Note 1:  There is no real need to employ the $FilePath variable.  The reason that I added this extra code was to draw your attention to the path, and highlight your need to change its value.

Note 2:  As expected, you need PowerShell’s signature tune the pipe (|) to separate a script’s main purpose of listing the events from out-File.

Note 3:  PowerShell cmdlets have a restricted number of verbs, ‘get’ and ‘set’ are particularly common; as you have probably guessed ‘out’ is short for output.

Challenge:  Change the value for $FilePath

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 2: Apply Get-Help to Out-File

Out-File is so intuitive that you don’t really need any help to get started.  Nevertheless, every time I apply get-Help to a command I learn something new.

Let us apply basic principles of PowerShell research to understanding the full capabilities of out-File.

# PowerShell script to investigate out-File
# Author: Guy Thomas
# Version 1.0 October 2008 tested on PowerShell v 1.0

get-Help out-File -full

Learning Points

Note 1:  Tip.  With PowerShell’s get-Help always append the -full parameter.

Note 2:  Get-Help reveals that the parameter in the first position is called -filePath.  Now I did not use this parameter explicitly in Example 1, instead I was lazy and just added the path directly after out-File.  My point: PowerShell is intelligent, and assumes the first string after the command is indeed the path, even though that idle-toad Guy omitted the -filePath parameter.

Note 3:  -append is an important parameter where you want to add text to an existing file.  Maybe you run the code again a week later, and want to preserve the original results rather than overwrite the file.

Note 4: -noClobber amuses me.  It means don’t overwrite the existing file. Translated, this means, ‘Guy, why don’t you subtly adjust the file name so that you don’t delete a precious result from last month’.

Note 5:  Whenever your run get-Help with the -full parameter the last lines of the help screen show sister commands.  In this instance I noticed out-Printer.  Handy for when I want a printout.  At the risk of kicking a man when he is down, redirecting a VBScript to a printer is nightmare for beginners.

Note 6: If you do try out-Printer it does not require any parameters.  PowerShell intelligently sends the job to the default printer.  However, should you want a different printer check get-Help out-Printer to research the -name syntax.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Conclusion Out-File

Out-File is easy.  I am deliberately cutting this ezine short to emphasise the idea of rewarding smart behaviour.  Whenever we want to save a PowerShell script to file all we need is the pipe symbol (|) out-File -filepath name.  That is it.  Your biggest danger is ‘overthink’.  You need nothing more to get started with out-File.

However, if I were still teaching, I would set my class the task of applying out-File to 3 different PowerShell commands.  If you are up for a little reinforcement try out-File with: get-Process, get-Service and get-WmiObject.  You can find examples of those commands in previous ezines.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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  • PowerShell -Filter   • Test-Path

PowerShell Get-ChildItem   • Get-ChildItem -Include   • Get-ChildItem -Exclude   • Compare-Object

PowerShell Registry  • Get-Credential  • PowerShell ItemProperty  • PowerShell ItemPropery GCI

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.