Ezine 175 - Out-File PowerShell's Killer Reason to Switch from VBScriptEzine 175 - PowerShell's Out-FileWhenever 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 SecretIf 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 MissionIn 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-FileIf 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.
Example 1: Out-File Saves Event Log Entries to DiskI 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 $FilePath = "E:\powershell\event50.txt" Learning PointsNote 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-FileOut-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 get-Help out-File -full Learning PointsNote 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)
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |