Windows PowerShell -f Format Operator
Introduction to Windows PowerShell -f Format OutputMy
mission on this page is to explain the basics of PowerShell's -f format operator. We use this operator, -f, to set the column widths in the output. Another way of looking at -f is to control
the tab alignment. It has to be said that Format-Table is the easiest method of controlling the output display; however, there are occasions where only -f can achieve the desired result.
Topics for PowerShell -f Format Operator
♣
I have chosen PowerShell's eventlog command to illustrate the formatting problem. What I would like is for the data in the three fields to align
precisely in columns. In the screenshot below,
data is difficult to read (even allowing for it being out of focus). Actually, it could be worse, the only reason there is a space between ACEEventLog and
OverwriteOlder is because I added the clumsy command: + " ". See code below.

Code
which Produces the Format Problem (Above)
# PowerShell -f format output example $EventVwr = Get-EventLog -List foreach ($Log in $EventVwr) { $Log.log + " " + $Log.OverflowAction + " " + $Log.MaximumKilobytes }

PowerShell -f code for Example 2
# PowerShell testbed. Example of the -f format output $EventVwr = Get-EventLog -List foreach ($Log in $EventVwr) { "{0,-28} {1,-20} {2,8}" -f ` $Log.log, $Log.OverflowAction, $Log.MaximumKilobytes }
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
Download a free trial of Solarwinds' Network Performance Monitor
What I would like to do here is explain how the -f format output operator works. I will give you examples to show every nuance, and every punctuation character. However, this is a practical rather than a technical explanation.
Let me begin with a reminder of the context. We are employing the command: Get-Eventlog -List, as a vehicle to experiment with the -f format operator. This script uses the standard loop
technique, and what we are particularly interested in is the alignment of the three properties: $Log.log, $Log.OverflowAction, and $Log.MaximumKilobytes In Example 2 (above),
we achieved the regular alignment with this format operator: "{0,-28} {1,-20} {2,8}" -f
Note 1: Each individual element in the output is enclosed by a set of braces {1,-20}.
The first number, zero, one or two, refers to the column in the output (first, second or third). If you study example 2, the first item, $Log.log, is referenced by zero in the first set of braces {0,-28}
Following the comma, comes the second number (-20 or 8), this determines the padding. Providing this number is larger than the number of letters in the longest data element, the columns
align nicely.
Note 2: Minus, and I emphasise
minus, -28 not only pads the element, but also makes sure that the first letters of each element line up vertically. Actually the best way to see what I mean is try the script WITHOUT the minus. The
reason that I am
hesitating to use the words left and right is because when I read a technical article on the subject, the technical author referred to left and right in the opposite way to my logic.
|