Wednesday, February 09, 2011

Formatting of Repeating Groups in PowerShell–Two Neat Tricks

When you use Format-Table to create a table from a pipeline/variable, you’ll find that PowerShell truncates the output of a column. Here’s a simple example

 

PSH [C:\foo]: Get-Process | Group company | Sort count | Select –Last 2 | Format-Table

Count Name                       Group
----- ----                       -----
   18 Microsoft Corporation     {System.Diagnostics.Process (conhost), ...
   51                           {System.Diagnostics.Process (ApMsgFwd),...

 

As you can see, PowerShell has truncated the Group colum to just show one member of the group. Note that ’ve deliberately truncated the width of the console to produce this output to avoid Blogger from ‘helpfully’ wrapping the column when you read this post – if you try the pipeline you’ll likely see a few more.

Here’s the first neat tric: To get more output you can use the –wrap parameter on the call to format Table

PSH [C:\foo]: Get-Process | Group Company | Sort Count | Select –Last 2 | Ft -wrap


Count Name                      Group
----- ----                      -----
   19 Microsoft Corporation     {System.Diagnostics.Process (conhost), System.Dia
                                gnostics.Process (conhost), System.Diagnostics.Pr
                                ocess (conhost), System.Diagnostics.Process (dwm)
                                ...}
   51                           {System.Diagnostics.Process (ApMsgFwd), System.Di
                                agnostics.Process (audiodg), System.Diagnostics.P
                                rocess (csrss), System.Diagnostics.Process (csrss

Now this better, but this still truncates the group to just 4 entries. What if you want to see more? To to that, and here’s the second neat trick, you need to set a preference variable, FormatEnumerationLimit. You set this variable to the number of group entries you want to see. By default PowerShell sets this to 4. If you set it higher, you will see something like this:

 

PSH [C:\foo]: $FormatEnumerationLimit = 10

PSH [C:\foo]: get-Process | group company | sort count | select -last 2 | ft -wrap


Count Name                      Group
----- ----                      -----
   19 Microsoft Corporation     {System.Diagnostics.Process (conhost), System.Dia
                                gnostics.Process (conhost), System.Diagnostics.Pr
                                ocess (conhost), System.Diagnostics.Process (dwm)
                                , System.Diagnostics.Process (explorer), System.D
                                iagnostics.Process (explorer), System.Diagnostics
                                .Process (ielowutil), System.Diagnostics.Process
                                (msnmsgr), System.Diagnostics.Process (msseces),
                                System.Diagnostics.Process (OUTLOOK)...}
   51                           {System.Diagnostics.Process (ApMsgFwd), System.Di
                                agnostics.Process (audiodg), System.Diagnostics.P
                                rocess (csrss), System.Diagnostics.Process (csrss
                                ), System.Diagnostics.Process (Idle), System.Diag
                                nostics.Process (inetinfo), System.Diagnostics.Pr
                                ocess (lsass), System.Diagnostics.Process (lsm),
                                System.Diagnostics.Process (MDM), System.Diagnost
                                ics.Process (mqsvc)...}

In this case, you new see 10 members of the group and not the default of 5. Were you to set $FormatEnumerationLimit to, say, 1000, then you would see up to 1000 entries. If you look at the variable (Get-Variable FormatEnumerationLimit | FL *), the description of this variable says: “Dictates the limit of enumeration on formatting IEnumerable objects”. This is a 32-bit integer so you could set it higher than 1000, although that's probably a bit OTT. Setting it to say 100 in your profile is probably good enough for most things.

Thanks to Alexandar for pointing me to this preference variable. For more information on this and the other PowerShell prefernce variables, you can get-help on about_perferencevariables.

1 comment:

Manoj said...

than you, that helped!