Share via


Adjust spacing for columns in Write-Output

Question

Tuesday, August 9, 2011 3:24 PM

I'm diving into using Add-Member and Write-Output to display some tables. However, the columns are spaced fairly far apart (especially when there are only a couple of columns). Is there a way to adjust the spacing, similar to using ft -auto?

All replies (6)

Tuesday, August 9, 2011 3:56 PM ✅Answered | 3 votes

You can use the -f format operator:

gci c:\testfiles |% {write-output $("{0,20} {1,30} {2,20}" -f $_.name,$_.lastwritetime,$_.length)}

and control the column widths and justification (left/right) in each column with the format specifiers.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Tuesday, August 9, 2011 3:32 PM | 2 votes

I figured it out. Instead of doing

Write-Output $obj

I use

$obj | ft -auto

 

Didn't realize you could do that.


Tuesday, August 9, 2011 3:37 PM | 2 votes

There's one catch though. When you pipe to ft each object is displayed as it's processed, one at a time. When you add the -Auto switch the formatter must calculate ALL objects before the result is displayed. Shay Levy [MVP]
PowerShay.com
PowerShell Toolbar


Tuesday, August 9, 2011 4:07 PM

...also, watch out if using format-table within a script; Output formatting can get messed up (in a most bizzarre way sometimes!)  Format-Table and the other format-* cmdlets aren't really intended for use in scripts (scripts should return objects:-) but if you need to use them then you should follow them with an Out-String

 

$obj|ft|out-string

 

HTH
Chris


Tuesday, August 9, 2011 4:55 PM

So what I'm doing is

foreach ($key in $clientHash.keys) {
$value = $clientHash.$key
 $CV = New-Object PSObject
 $CV | Add-Member NoteProperty "Client Version" $first 
 $CV | Add-Member NoteProperty "Agent" $second
 $CV | Add-Member NoteProperty "Connections" $value 
 $CV
}

The other problem I'm having is that after the above runs, I have another section

foreach ($key in $serverHash.keys) {
 $value = $serverHash.$key
 $obj = new-object PSObject
 $obj | Add-Member NoteProperty "FrontEnd Server" $key
 $obj | Add-Member NoteProperty "Connections" $value
 $obj
 # "{0, -22} {1, 5}" -f $key, $value
}

The first gives me a table, albeit with columns far apart. The second only gives me the last column ("Connections"), and it's lined up with the Connections column of the first table. No headers. So I think the second foreach is thinking it's part of the first. The two should be completely separate tables.

What am I missing?


Saturday, August 13, 2011 6:39 AM

Hi Pat,

Please feel free to let us know if the information was helpful to you.

Regards,

Tiger Li

TechNet Subscriber Support in forum
If you have any feedback on our support, please contact  [email protected].

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.