Many times you may want to save the results of a cmdlet to a text file. In PowerShell this is easily accomplished with redirection using > or >>. In Chapter 1 we saw that > redirects console output to a file and overwrites any existing file, while >> appends to an existing file. If you use >> and the file doesn't already exist, then the file will be created:
PS C:\> get-wmiobject -class win32_computersystem >audit.txt PS C:\> get-wmiobject -class win32_operatingsystem >>audit.txt PS C:\> get-content audit.txt Domain : SAPIENPRESS Manufacturer : Dell Computer Corporation Model : Latitude D800 Name : GODOT PrimaryOwnerName : Administrator TotalPhysicalMemory : 1609805824 SystemDirectory : C:\WINDOWS\system32 Organization : TestCo BuildNumber : 2600 RegisteredUser : Administrator SerialNumber : 55274-640-1614466-00000 Version : 5.1.2600 PS C:\>
In this example we've sent the output of the Get-Wmiobject cmdlets to a text file called audit.txt. The first command creates the file and the second appends to the file. We'll examine other ways to control what type of output PowerShell produces, all of which you can save to a text file using console redirection.
PowerShell includes a cmdlet that sends output to a text file. When saving output to a file, this cmdlet is easier to use in a script instead of trying to use redirection. You will use this cmdlet in a pipeline:
PS C:\> Get-service |out-file c:\logs\audit.txt
If you open c:\logs\audit.txt, you'll see the results of the Get-Service cmdlet. The cmdlet also makes it easy to append to an existing file:
PS C:\> Get-process |out-file c:\logs\audit.txt -append
Unlike redirection, you can also instruct Out-File to not overwrite an existing file:
PS C:\> get-service |out-file c:\logs\audit.txt -noclobber Out-File : File C:\logs\audit.txt already exists and NoClobber was specified.
You can see that PowerShell refused to overwrite an existing file. However, it's is easy enough to force PowerShell overwrite an existing file:
PS C:\> get-wmiobject -class win32_logicaldisk |out-file c:\logs\audit.txt -force PS C:\> get-content c:\logs\audit.txt DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 2815569920 Size : 15726702592 VolumeName : Server2003 PS C:\>
Here we sent the output of the Get-Wmiobject cmdlet to the same file and forced it to overwrite the existing file.
One of the many new PowerShell features is the ability to send a cmdlet's output directly to a printer using the Out-Printer cmdlet:
PS C:\> get-wmiobject -class win32_logicaldisk |out-printer
This command sends the output of the Get-Wmiobject cmdlet directly to the default printer. If you have more than one printer, you can specify a printer by name:
PS C:\> get-wmiobject -class win32_logicaldisk |out-printer "Adobe PDF"
In this instance we're sending the output the virtual printer that is installed with Adobe Acrobat. This results in a new pdf with the output of the Get-Wmiobject cmdlet.
If you want to send output to a network printer, simply specify the printer UNC:
PS C:\> get-process |out-printer "\\Print01\HPLaserJ"
As we've seen so far with the redirection examples, if we send the output to a file, we don't see the output at the console. What if we want to do both? Using PowerShell's Tee-Object cmdlet we can view the output and send it to a text file:
PS C:\>Get-process |tee-object c:\processes.txt
This expression displays the results of the Get-Process cmdlet and sends them to the text file. Unfortunately, the Tee-Object doesn't support sending output to a printer.