Hi there
have a quick question
I have a script that checks for messages send/received on servers
pretty simple and it works fine.
I collect all the data to a custom object(server,send,receive)
then output it like this:
$data |Ft -autosize
(I tried other format table kind of views)
the output I get is like this:
Server Send Receive
server1 2 5
server2 10 15
my question is is there away to align the numbers to the left instead of to the right?
of course it isn’t a big deal I just want to learn new things so im interested to know if
its something simple
Thanks
Most people only use Name (or Label) and Expression when using the hashtable syntax for constructed properties in Format-Table, but you can also set Width and Alignment in there as well.
I’m not sure what you want me to be focusing on, there. The only difference I see (as related to the Format-Table command) is the use of the -Wrap switch, and different property names on the input objects (ReceivedMessage and SentMessages, instead of Receive and Send.) The same idea should work; you just need to pass something to the -Property parameter of Format-Table, and the objects passed to that parameter can be hashtables which define the Alignment and/or Width of a column. (Width is pointless if you’re using -AutoSize, but you get the idea.)
That can go just before the call to Format-Table; no need to put it inside a loop.
BTW, found the documentation I was looking for: http://technet.microsoft.com/en-us/library/hh849892.aspx . It just doesn’t show up when you run Get-Help Format-Table -ShowWindow, due to a slight problem with that command (only showing the first paragraph of each property’s description.) If you run Get-Help Format-Table -Full , the documentation does show up in the console. In addition to the Alignment and Width keys, there’s one other that I didn’t mention earlier: FormatString. Example:
$data = @(
[pscustomobject] @{
Date = Get-Date -Year 2008 -Month 12 -Day 31
}
[pscustomobject] @{
Date = Get-Date -Year 2012 -Month 6 -Day 13
}
)
Write-Verbose -Verbose 'Default date / time formatting for current culture'
$data | Format-Table -AutoSize | Out-Host
Write-Verbose -Verbose 'Specifying format string ("D" for long date pattern of current culture)'
$properties = @{ Label = 'Date'; Expression = { $_.Date }; FormatString = 'D' }
$data | Format-Table -Property $properties -AutoSize | Out-Host
Write-Verbose -Verbose 'Specifying custom format string'
$properties = @{ Label = 'Date'; Expression = { $_.Date }; FormatString = 'ddd, MMM d yyyy' }
$data | Format-Table -Property $properties -AutoSize | Out-Host
Another bit of useful information from that documentation… the Expression can just be a string, if all you’re doing is accessing a property on the input object. No need to write { $_.Date } when just ‘Date’ will do: