Format-table, or ft for short, controls the output of your Windows PowerShell commands. Whenever presentation of information is important, pipe the script's output into format-Table. On this
page I will show you examples of this straightforward, but important command.
Scenario: we wish to investigate processes running on an XP workstation or a Windows Server.
Pre-requisites: PowerShell and .Net framework are
installed on your XP or Windows Server You are at the prompt PS>
Example 1a Let us get started, type: get-Process
Example 1b Now let me introduce format-Table so
that we can control the output columns. 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.
As regards our working technique for format-Table, we have reached a crossroads. My preferred working method is to create scripts and then run them from the PowerShell command line. The other
alternative is to keep typing and re-typing the commands in the shell. My technique comes into its own when the commands are complex; as a bonus my scripts document what I do so it's easy to refer and
refine previous experiments.
If you too like this script (cmdlet) method, then first make sure PowerShell will allow script to run, you only have to do this once, type : set-executionpolicy
RemoteSigned
Assuming that I have saved example 2 in a file called proc.ps1, what I do is go to the PS prompt and type .\proc. You either have to save the script into the working directory, or
else use cd to change to the directory where the script was saved.
Guy Recommends: SolarWinds Engineer's Toolset v10
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates.
There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10
Example 2a: Time to see
which properties of get-Process are available, then we can fine tune our format-Table command. get-Process | get-Member -Membertype property
Example 2b: Even here, I cannot resist using format-Table to
filter which column gets exported to the file. $Proc
= get-Process | get-Member -Membertype property $Proc | format-Table name | out-file procprop1.txt
Note: It's not really necessary to introduce a variable, $Proc. However, one advantage of
this technique is that it
saves problems with our script word-wrapping to the next line.
Example 3a: Let us choose some different columns in the output, for example, BasePriority and HandleCount: get-Process | format-Table name, handlecount,
basepriority
Example 3b: Let us see what happens is we add -auto. get-Process | format-Table name, handlecount, basepriority -auto
Example 4: Suppose you want some order in your output, no problem, call for Sort-object. $Proc = get-Process |sort-Object -descending basepriority
$Proc | format-Table name, handlecount, basepriority -auto
Note: -groupBy This parameter offers a different method of aggregating the data.
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 these PowerShell Aliases, 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-list you will extend your range of formatting options.
Example 1c [Use in conjunction
wit Example 1a and 1b above] get-Process | ft processName, handles, PagedMemorySize -auto
Learning points. You can substitute ft for format-Table.
Also you can research other properties, for example PagedMemorySize.
Example 2c get-Process | get-Member -Membertype method |ft 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 5b: get-Process | ft name, handlecount -groupby basepriority -auto
Learning points. It's not essential to use variables. This is a simpler example focusing on the -groupby switch.
In addition to
format-Table, 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.
The other formatting possibility is a list. This is useful when you want to display multiple properties for one
object. Here is an example: get-Process services |format-list -property *
Note: What makes using the format-list worthwhile is the * (star) wildcard.
Presentation really does transform the output of your
PowerShell scripts. Also, sometimes you get too much information and the answer is to filter the columns. 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-Table (or ft 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.
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.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.