Home QuickTipps Displaying Multi-Column Strings
 

Displaying Multi-Column Strings

Displaying Information in Multiple Columns

Format-Wide can display information in multiple columns. Simply specify the property you want to display and the number of columns you want:

The next line gives you a 4-column display of all running processes:

Get-Process | Format-Wide -Property Name -Column 4

Displaying Strings And Numbers

Unfortunately, Format-Wide only works with rich objects but not with primitive data. The reason is simple: you need to specify a property to display, and since primitive data like strings and numbers have no properties, Format-Wide cannot be used.

PowerShell MVP Kirk Munro pointed out, though, that you can use a workaround by using a hash table (calculated property). This also requires the parameter -Force. So to display six random lottery numbers as two columns, use this (just adjust (1..49) to the numbers used by your favorite lottery):

Get-Random -InputObject (1..49) -Count 6 | 
  Sort-Object | 
  Format-Wide -Column 2 -Property @{Expression={$_}} -Force

Combining Multiple Properties

Since Format-Wide can only display exactly one property, its use is sometimes limited. With the technique above, you can easily combine content of multiple columns (properties) in one string, though. The next example lists all running processes plus their process ID in a 3 column display:

Get-Process | 
  Format-Wide -Column 3 -Property @{E={'{0} ({1})' -f $_.Name, $_.ID}} -Force

Note that inside the hash table, you can shorten “Expression” to “E”. In this example, we used the operator -f to create the output string. On the left side, you see the text template with its two placeholders {0} and {1}. On the right side, you see the two columns that are to be placed into the placeholder.

The result: each process is displayed by name, followed by its process ID in parenthesis.