Format the output of a string in multiple columns with PowerShell
In my previous blog article, I used the PowerShell Format-Wide
cmdlet to format the output of a
string in multiple columns. While Format-Wide
isn't a command that I've used extensively, the
behavior wasn't what I expected.
When you pipe object-based output other than a string from any PowerShell command to
Format-Wide
, it produces the desired results.
1Get-PSDrive | Format-Wide -Column 2
I'll pipe the following to Get-Member
to confirm that it's a string.
1'one', 'two', 'three', 'four' | Get-Member
When you use Format-Wide
with a string, it doesn't split the results into multiple columns.
1'one', 'two', 'three', 'four' | Format-Wide -Column 2
More testing and still not the results I expected.
1'one', 'two', 'three', 'four' | Format-Wide -Column 2 -Force
2'one', 'two', 'three', 'four' | Format-Wide -Property $_ -Column 2
3'one', 'two', 'three', 'four' | Format-Wide -Property $_ -Column 2 -Force
4'one', 'two', 'three', 'four' | Format-Wide -Property {$_} -Column 2
To produce output with multiple columns from a string using Format-Wide
, you need to specify the
current object variable inside curly braces as the value for the Property
parameter along with the
Force
parameter.
1'one', 'two', 'three', 'four' | Format-Wide -Property {$_} -Column 2 -Force
The following video demonstrates the commands used in this blog article.
Jeff Hicks published a follow-up blog article featuring a function to format the output of strings in multiple columns with PowerShell.
µ