Windows PowerShell Out-FileIntroduction to Windows PowerShell Out-File
There are many occasions when you need to output the results of a script not to the screen, but to a file. In the old days with DOS, we could use the greater than symbol: '>'. However, to
redirect PowerShell output we need to
master this construction: Topics for PowerShell Out-File
Example 1 To Record Methods and PropertiesFrom the myriad of possible examples, I have selected the ComObject named InternetExplorer as a test-bed for our out-File commands. Let us consider the following situation, you employed | get-Member to review an object's methods and properties, but now you wish to record the output in a file. Here is a classic job for the out-File construction. Note in passing the use of the pipeline (|) symbol. As usual, its job is to take the output of the left hand expression and make it the input of the right hand expression, in this instance the upshot is that the object's methods and properties are saved into a named file. Task: To List the InternetExplorer MethodsThe scenario: We want to employ PowerShell for scripting the InternetExplorer application, but we are not sure which methods are available (and how to spell them). Instructions Naturally, you need to install PowerShell as a pre-requisite. My advice is to get into the habit of creating cmdlets, thus, save block d) below into a text file with .ps1 extension, for example ie7.ps1. Then launch PowerShell and navigate to the folder where you saved ie7.ps1. Now issue the command .\ie7. Finally open the file created by your cmdlet, iemethod.txt, and compare it with the results below. a) Create the InternetExplorer object. b) Add the |get-Member instruction. c) Filter for just the Methods -MemberType
-method. d) Finally, append the crucial section: |out-File iemethod.txt. PowerShell cmdlet demonstrating out-File The result: A list of InternetExplorer’s methods, as observed by opening iemethod.txt Name MemberType Definition Learning PointsNote 0: If you wanted the script to actually open the internet explorer, append this command: Note 1: As ever, Microsoft in general and PowerShell in particular, provide multiple way of achieving the same objective. For instance you can use -ComObject instead of the depreciated -com. Note 2: Talking of shortened commands, you could dispense with creating a variable and just use: Note 3: If you are new to PowerShell, then the pipe (|) instruction enables you to filter the output of the first command. In this example we issued two | 'pipe' instructions, one to get the properties, and one to redirect the output to a file.
˚
Example 2 out-File to Record Services RunningHere is a simple example of employing out-File to record the services actually running on a computer. Preliminary Stage: Display Running services Final Stage out-File # PowerShell cmdlet to save service to a file Note: Try this alternative to {$_.status -eq "Running" } --> {$_.status -eq "Stopped" } Out-PrinterThere is also a sister command called out-Printer. If you try: Then you can see that you don't need to specify the name of the printer, PowerShell automatically selects the default printer, just as any application would. You could experiment by substituting out-Printer for any of the above out-File commands Summary of Windows PowerShell out-FileIf you need a permanent record of your script’s output, then the key command is: | out-File filename.txt. Another possible use of this out-File command is for keeping a list of an object’s methods and properties. My advice is to save the instructions, including | out-file, into a cmdlet, which you then reuse. See Also PowerShell Tutorials:• PowerShell Home • Com • Shell Application • Active Directory • QAD Snap-in • Get-Member 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.
*
|
||||||