2
votes

I use Powershell read various XML config files and then display the output in a nice tabular format using the format-table -autosize command and output as a text file.

Since the config files change every now and then, I update these text files within our SVN repository each day, if there are any changes. What's really nice is that I can go back and look at history as to when a particular config change occurred.

One of the issues I have, however, is sometimes the config entries are "wider" than previous days and therefore causes the columns from one day to he next to shift wider than before.

My question is... if there is a way for me to specify a default width for each column within the format-table command versus relying on Powershell to adjust automatically.

I realize I could use format-table or format-table -autosize.... and I found using -autosize seems to be better because otherwise each line and the spacing between columns is very wasteful.

Any ideas or ways to get around this would be appreciated. Most of the configs have at least 10 columns or more.... usually containing URLs, port numbers, etc.

1

1 Answers

5
votes

You can use a hashtable to specify the width e.g.:

Get-Process -id $pid | 
  ft -prop @{n='Name';e={$_.Name};width=20},@{n='Id';e={$_.Id};w=8},@{n='PM';e={$_.PM};w=18}

Note that n is short for Name, e is short for Expression and w is short for Width. You can use either the short or the long form of these names. For completeness, there is an alignment field (a for short) that can be either Left, Center or Right. These settings are documented in the Format-Table help under the information on the Property parameter.