Controlling PowerShell's Results with Out-FileControlling PowerShell's Results with Out-FileViewing a script's output on screen is all well and good, but often it's more convenient to save PowerShell's results to a file. Enter PowerShell's command: 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. Out-File Topics
Introduction to Out-FileAppending out-File to an existing command is easy. 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 output into a file with a command such as: | out-File c:\ logs\result1.txt. Example 1 - Starting with Out-FileThis 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 script to list the files in C:\Windows\System32 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-HelpOnce 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. Get-Help out-file -full If you append the -full switch, then PowerShell's help reveals useful parameters, for example, -filepath (taken for granted) -append (forgotten) -NoClobber (previously overlooked). Example 2 - Putting Out-File to WorkThis 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 Note 1: 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 2: 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 3: 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. -Append | Out-File -filepath "D:\Files\dll.txt" -append -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 1: 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. Content FamilyOut-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) Out-PrinterOut-File has 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 you would print from any other application. You could experiment by substituting out-Printer for any of the above out-File commands Export-Csv also Import-CsvSpreadsheets 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. Summary of PowerShell's Out-FileTrust me, you will make great use of PowerShell's out-File for saving results to a text file. This is a straight-forward command to bolt-on to existing commands. The main danger for newbies is looking for non-existent commands; remember that PowerShell takes care of both file open and file close. 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.
*
|
||||||