PowerShell Ezine, Logon Scripts

Ezine 175 - Out-File PowerShell's Killer Technique 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


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

ˆ

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 SolarWinds ipMonitor

For more network research, I recommend ipMonitor.  My attraction to ipMonitor is because it inhabits that zone of part work, part play; Guy just could not put the dashboard away.  This excellent performance monitor will get you started in the quest to remove bottlenecks on your network.  SolarWinds provides this fully-functioning product free for 21 days.  So download and install ipMonitor, then start scrutinizing your computers Network, CPU, memory and disk performance.  You can also select from zillions more performance counters such as fan temperature and battery level.

Download SolarWinds ipMonitor (21 days eval)

See More Microsoft PowerShell WMI Examples:

Home   • PowerShell WMI   • WMI Services   • Memory   • Disk   • DNS   • Win32_pingstatus

WMI Class   • Win32_NetworkAdapter   • Win32_NetworkAdapterConfiguration   • Disable NIC

Examples   • Win32_product   • PowerShell -Query   • PowerShell -Filter

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.

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.

 *


Google

Web  This website

Review of Orion NPMGuy 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.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.