Format-List, or FL for short, allows PowerShell to control the output of
your main script. Whenever presentation of information is important, pipe the script's output into
Format-List
(or format-Table).
On this
page I will show you examples of this straightforward, but important command.
Scenario: We wish to investigate
the DHCP service running on workstation or a Windows Server.
Pre-requisites:
PowerShell and .Net framework are installed on your computer. You are at the prompt PS>
Or if you have PowerShell 2.0, launch the ISE.
Example 1a
Let us get started with an example using the default formatting:
# Preliminary example no Format-List yet Get-Service
The 'problem' is that we want to home in on just the DHCP
service, furthermore we want to display more properties.
Example 1b Now let me introduce Format-List so that we
can better control the output.
# Example of PowerShell Format-List Clear-Host
Get-Service DHCP | Format-List
Note 1: Thanks to Format-List we now have information about
DHCP that we needed.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
About the only thing that can wrong with Format-List is forgetting the
pipe (|) before the command. The secret is to get the main command
working then use (|) to pipe the output into Format-List. From
there you list the names of the properties you wish to display.
For many cmdlets, especially those with more than 5 properties,
Format-List is the default method of displaying the data. Thus it's
often a case of, 'How do I choose the properties or columns and then
display them with Format-Table', this is especially true when working
with Get-WmiObject classes.
Format-List Alias FL
With Microsoft, there are always at least three ways of doing everything,
what seems like redundancy when you are an expert, seems like perspective
when you are a beginner. One obvious example is that you can
abbreviate format-Table to ft. As you increase your range of
PowerShell commands, keep an eye out for another
PowerShell Alias, for example gci (Get-Childitem).
Here are alternative methods of achieving the above objectives, each
example is designed to develop your binocular
vision, hence see the target more clearly. For example, if you experiment with format-wide and format-Table you will extend your range of formatting options.
# Example of PowerShell Format output with ft Clear-Host
Get-Service | Where {$_.status -eq 'Running'} |fl Name, CanShutdown
Learning points. You can substitute FL for
Format-List. Actually, lower-case fl works just as well, I use
the capital form because l could be mistaken for I. You can
also use this as an opportunity to reduce the number of properties
displayed in the output.
Example 2 Get-Member
Clear-host Get-Service | Get-Member -Membertype method |fl
name
Learning points. In addition to property, you can
research an object's method. For instance, in other scripts you may wish to employ the .kill() method.
Example 3 Real-Life Example to Detect Rogue .exe Files
Scenario, you want check C:\Windows for non-Microsoft files.
Learning Points. This script will list all
executables in the Windows folder that don't belong to Microsoft.
I thank Gary Mitchell for sending this PowerShell script.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion 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.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is 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 take advantage of Solarwinds' offer.
Scenario: we wish to investigate processes running on workstation or a Windows Server.
Pre-requisites:
PowerShell and .Net framework are installed on your computer. You are at the prompt PS>
Or if you have PowerShell 2.0, launch the ISE.
Example 1a
Let us get started with an example using the default formatting:
# Preliminary example no Format-Table yet Get-Process
The 'problem' is that the name of the process is on the
right, and we have columns in the output which are of no interest.
Example 1b Now let me introduce Format-Table so that we
can control which properties to display in the output. Let us
suppose that we are only interested in 'Handles'.
# Example of PowerShell Format-Table Get-Process |
Format-Table processName, handles -auto
Note 1: Thanks to Format-Table we now have the name of the process in the first column.
Note 2: -auto prevents the output spreading to the full width and consequently, the -auto parameter makes the figures easier to read. To see the effect try the above command without -auto.
Guy Recommends: Solarwinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
In addition to
format-List, you can display data in not one column but two or three columns. This is the format-wide or (fw) option, which is useful where you have a long list with only one field, for example:
Get-Process | Get-Member -Membertype method
|format-wide name -column 3.
»
Summary of Format-List in Windows PowerShell
Presentation really does transform the output of your
PowerShell scripts. Also, when you get too much information the answer is to filter
and reduce the number of columns in the display. On other occasions, you need to display extra properties, which are not shown by the default command. In
each case, format-Table gives you control over the presentation of your output.
While format-List
(or FL for short), is a ubiquitous command, it does have numerous switches. With judicious application of its
many switches, you can produce creative and effective outputs.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.