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 ExamplesScenario: we wish to investigate processes running on an XP workstation or a Windows Server. Pre-requisites: Example 1a Example 1b 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) TechniqueAs 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 : 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 examplesExample 2a: Example 2b: Even here, I cannot resist using format-Table to
filter which column gets exported to the file. 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: Example 3b: Let us see what happens is we add -auto. Format-table - Advanced examplesSort-object (Can be abbreviated to Short) Example 4: Note: -groupBy This parameter offers a different method of aggregating the data. Example 5:
˚
PowerShell PerspectiveWith 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] Learning points. You can substitute ft for format-Table. Also you can research other properties, for example PagedMemorySize. Example 2c 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: Learning points. It's not essential to use variables. This is a simpler example focusing on the -groupby switch. Format-WideIn 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: Format-ListThe other formatting possibility is a list. This is useful when you want to display multiple properties for one
object. Here is an example: Note: What makes using the format-list worthwhile is the * (star) wildcard. Summary of Format-table in Windows PowerShellPresentation 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.
*
|
||||||