Windows PowerShell


Windows PowerShell - Format-table (ft)

Windows PowerShell Scripting - Format-table (ft)

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.

Windows PowerShell Format-table Topics

Format-table - Simple Examples

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.

Script (cmdlet) Technique

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.

Format-table - Intermediate examples

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

Format-table - Advanced examples

Sort-object (Can be abbreviated to Short)

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.

Example 5:
$Proc = get-Process |sort-Object -descending basepriority
$Proc | format-Table name, handlecount -groupby basepriority -auto

  ˚

PowerShell Perspective

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.

Format-Wide

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.

Format-List

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.

Summary of Format-table in Windows PowerShell

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.

See more Microsoft PowerShell syntax

PowerShell Home  • Syntax  • -f format  • Pipeline  • Quotes  • Format-table  • Group  • Select-String

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.