Thursday, November 19, 2009

Messing with output from Format-Table

Found at question at

I was wondering if I could write the outout without any blank spaces between 2 fields. For example, I am using hash tables to display 2 columns from dir output. But it always comes up with a blank space between those 2 data elements. I need this to generate a fixed format output with data elements only and no spaces in between. Any help is greatly appreciated.

$column1 = @{expression="mode";width=5;label="mode";alignment="left"}
$column2 = @{expression="name";width=10;label="name";alignment="left"}

$dir |format-table $column1,$column2

$mode  name
----  ----
d---- download
d---- extract
-a--- alias.txt
-a--- Compute...
-a--- execute...
-a--- get_dn.ps1
-a--- hh

Well, it can be done. I looked into the objects the Format-Table spit out and after some poking around, I came up with this -

$column1 = @{expression="mode";width=5;label="mode";alignment="left"}
$column2 = @{expression="name";width=20;label="name";alignment="left"}

# Save widths, all non-fixed length value should specify a width
dir | format-table $column1,@{l="|";e={"|"}},$column2 | foreach {
if ($_.pstypenames[0] -eq "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData") {
# Capture the values and convert them to one value
foreach($i in 0..($count-1)) {
# Delete all but one field
# and update its value
elseif ($_.pstypenames[0] -eq "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData") {
# Capture the headers and convert them to one header

foreach($i in 0..($count-1)) {
# Delete all but one field
# and update its value

If you like it, convert it to a function as en exercise ;)


Happy formatting!


Jason Archer said...

To answer the user's question, it would be far easier to do this:

dir | ForEach-Object {"Mode Name";"---------"} {"{0}{1}" -f $_.Mode,$_.Name}

I used string formatting so that you can add padding like so: "{0,-10}{1}"

Now, if you wanted to do this for any ole object, then it would be more worth while to let Format-Table do the dirty work.

Per Østergaard said...


Your suggestion was also my first reaction, but to still be able to utilize Format-Table and to let people use what they already know, I decided to look a way to manipulate the output from Format-Table.