How to increase spacing between columns with Format-Table?

I posted on stackoverflow, but haven’t gotten an answer: format table separator

By default, PowerShell’s Format-Table uses a single space character as a column separator. Depending on the data, it can result in somewhat of a jumbled mess. PowerShell should allow for the column separator to be overridden in some way (e.g. 3 spaces instead of 1).

Is there a way to do this? If not, are there any decent alternatives to Format-Table that would provide this functionality?

Kyle
Welcome to the forum. :wave:t4:

Not that I’m aware of.

I’d think it’s designed more to check some data and not to be presented in a nice way. If you need to present it I’d recommend other ways anyway like an Out-GridView for example or a nicely formatted Excel sheet you could create with the great module from Doug Finke InportExcel.

The -f Format operator allows you to change alignment a number of characters to the left or right, the column header remains in the same place though but it might work for you.

In ‘{0, 0}’, the second number determines the horizontal space, a negative number moves characters to the left and a postive number moves the characters to the right

PS C:\> '{0,0}' -f 'Hello World!'
Hello World!

PS C:\> '{0,30}' -f 'Hello World!'
                  Hello World!

Or try something like this.

Get-Process | select -First 10 -Property ProcessName, Name, @{N = 'Id'; E = {'{0,20}' -f $_.Id}}

… tweaking the output system of PowerShell could be a way to accomplish what you need though.

You may start reading about with this:

and maybe this:

Thanks for the replies and suggestions! You have confirmed my suspicion. :frowning:

I have used the format.ps1xml approach and the select expressions approach and they both work, but have short-comings.

The select expression approach is fine for a one-off thing, but not if you have to do it for every column you want to display in the table (just to add extra spaces between columns). And it is not easily typed out.

The format.ps1xml approach is awesome for ultimate customizability, but it is over-kill for basic usage and it won’t work correctly in some situations (e.g. column data for one or more records is longer than the defined width for that column). You end up either wasting a lot of space between columns or only have a single space, which is no better off than you were to begin.

See below for an example of how it works today (1 space), how it could be (3 spaces) and (5 spaces) with a simple parameter. It is objectively easier to consume. I’m doing things like this all the time to inspect my data and sometimes to send it to team members (copy/paste or snip).

$containers | Format-Table

Id           Name        Created          Status      IPAddress        Pid Platform Image     Command
--           ----        -------          ------      ---------        --- -------- -----     -------
c311319af3c0 pwsh-nano-1 2021-12-11 15:25 Up 29 hours 192.168.99.43  27248 windows  pwsh-nano "ping -t localhost"
c911d82df957 pwsh-2      2021-12-05 16:30 Up 3 days   192.168.99.235  5420 windows  pwsh      "ping -t localhost"
$containers | Format-Table -SeparatorSpaces 3

Id             Name          Created            Status        IP Address       PID     Platform   Image       Command
--             ----          -------            ------        ----------       ---     --------   -----       -------
c311319af3c0   pwsh-nano-1   2021-12-11 15:25   Up 29 hours   192.168.99.43    27248   windows    pwsh-nano   "ping -t localhost"
c911d82df957   pwsh-2        2021-12-05 16:30   Up 3 days     192.168.99.235   5420    windows    pwsh        "ping -t localhost"
$containers | Format-Table -SeparatorSpaces 5

Id               Name            Created              Status          IP Address         PID       Platform     Image         Command
--               ----            -------              ------          ----------         ---       --------     -----         -------
c311319af3c0     pwsh-nano-1     2021-12-11 15:25     Up 29 hours     192.168.99.43      27248     windows      pwsh-nano     "ping -t localhost"
c911d82df957     pwsh-2          2021-12-05 16:30     Up 3 days       192.168.99.235     5420      windows      pwsh          "ping -t localhost"

To me, it seems like a lot of value for something that can’t (or at least shouldn’t) be that difficult for the MS PowerShell team to implement. They already have to be calculating the max length of any value in each column so they figure out the spacing for display, and they are just adding 1 additional space between columns. Instead of adding 1 space, add the number of spaces provided by the SeparatorSpaces parameter (or the global default, if the parameter wasn’t provided). The global default would be 1, so it would be backward compatible (no breaking changes).

What would be the best way to go about advocating for this feature enhancement?

Well, that’s annoying. I typed up long post with examples and then had to edit a word, and now it’s just gone. Apparently was hidden by some spam filter. That was over half hour ago.

I could edit that post so it would say “typed up a long post” instead of “type up long post”, but… then it would be gone. Not a great user experience…

Have a look at the -Property parameter in the Get-Help for Format-Table.

$csv = @'
Name,Colour,Classification
Apple,Green,Fruit
Orange,Orange,Fruit
Potato,Brown,Tuber
Tomato,Yellow,Fruit
Courgette,Green,Vegetable
Carrot,Orange,Vegetable
'@

$csv = $csv | ConvertFrom-Csv

$csv | Format-Table -Property @{e='Name'; Width = 12; Align = 'Left'},
                              @{e='Colour' ; Width = 9; Align = 'Left'},
                              @{e='Classification' ; Width = 15; Align = 'Center'}
2 Likes