Showing posts with label -f operator. Show all posts
Showing posts with label -f operator. Show all posts

January 14, 2010

Script Output - Formatting with -f
[Align your columns the way YOU want]

This article will discuss absolute positioning of Powershell screen output. There are times when the standard formatting of Powershell output just won't cut it. Enter the -f formatting operator. Until you get familiar with its syntax, the use of this operator can be somewhat cryptic. In the following discussion, I'll attempt to shed light on the many options available and show how we use it for output formatting.

Consider the following code:

$All_MBXs = Get-Mailbox
foreach ($MBX in $All_MBXs) {write-host $MBX.Name,`

$MBX.PrimarySmtpAddress}


and the resulting output:


If we replace the write-host cmdlet using the -f output formatting operator, we can produce the following output:

The code change to produce the output above is shown below. Note that the Write-Host cmdlet is completely replace with the -f operator construct.

$All_MBXs = Get-Mailbox
foreach ($MBX in $All_MBXs) {"{0,-14} {1,-25}" -f `
$MBX.Name, $MBX.PrimarySmtpAddress}


In looking at the code in the example above, we use {} to encapsulate the formatting specifiers, then -f to indicate the start of data items to format.


Formatting Details Explained

"{0,-14} {1,-25}"


The first set of curly braces {} define what data to display and the column padding to use for it. In this example, data object zero, $MBX.Name (the first item defined by the object's position as it appears after the -f ) will be output with a column width of 14 . The minus sign preceeding the column width of 14 indicates that the data should be "left" justified... leaving it off will "right" justify that piece of data when output. [Note: Using a plus sign rather than no sign will produce an error in your script].


What we are building is a formatting template (note parenthesis surrounding the entire "template") that will be used by the -f operator when it decides how to display the ouput. Also worth mentioning here is that the space between the two sets of braces IS relevant. For example, if you insert ten spaces between the two sets of braces, the -f operator will interpret that as space that you want to show in the output.


The second set of curly braces in the example indicate that data object one (the second item in the list after the -f) will be output with a column width of 25, right justified.


The use of the -f operator to product custom output is a powerful and relatively easy tool to produce great looking and easily readable output. There are several other formatting options using this method, such as percent and currency specifiers, numeric formatting, partial date/time specifiers for hours, minutes, days, etc. I won't go into those now, but may in the future post.


The best way to learn this formatting option is to play with some code and see how it affects your output. Enjoy and happy scripting!

.end


more...