Controlling PowerShell's Results with Out-File
PowerShell Write To Text File - Out-File
Viewing a script's output on screen is all well and good, but often it's more convenient tell PowerShell to write the output into a text file. This a task for 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.
Topics for PowerShell Output to File
Writing the results of a command into a file is easy with PowerShell's Out-File. 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 PowerShell output to a file with a command such as:
This 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 write to text file
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.
Once 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, so it is with PowerShell's write to text file.
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).
SolarWinds' Network Performance Monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
This 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 2: 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 3: 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 4: If you get an error message 'Cannot find part of the path', then amend D: \files to a real folder on your machine.