tags:

views:

174

answers:

2

I have the following array value $outData with several columns. I am not sure how I align some columns right?

$outData | Select-Object `
      Name `
      @{Name="Freespace(byte)"; Expression={"{0:N0}" -f $_.FreeSpace}}, '
      .... # other colums `
 | Format-Table -AutoSize

It works fine. However, when I tried to use align for the freespace column to right:

      @{Name="Freespace(byte)"; Expression={"{0:N0}" -f $_.FreeSpace}; align="right"}, '

I got error message "Specified method is not supported". Not sure if there is any way to align the value to right?

+2  A: 

The align directive goes in a hashtable that is specified to the Format-Table cmdlet. IOW, align is not a supported hashtable entry for Select-Object. So make sure to do your formatting via hashtables in the hashtable passed to Format-Table e.g.:

gps | select name,pm | format-table @{n='Name';e={$_.Name};align='right'},PM

or in your case:

$outData | Format-Table Name,
                  @{n="Freespace(byte)";e={"{0:N0}" -f $_.FreeSpace};a="right"}
Keith Hill
I tried Select with a="right", but it is still not working. However, if I remove it, the Select works fine (as same as Select-Object).
David.Chu.ca
by the way, my $outDate is initialized like $outData=@{""}; and populated from wmi objects
David.Chu.ca
Reread the proposed answer. I had a copy/paste error. :-) The align directive is *not* supported for hashtables used with Select-Object. It is supported for hashtables used with Format-Table.
Keith Hill
A: 

Given the assumption that the source data is wmi ... this works:

gwmi win32_logicaldisk | 
    ft Name, @{n="Freespace (byte)";e={"{0:N0}" -f $_.FreeSpace};align='right'}
xcud
I copied the line to ps. I got error "ft<<< specified method not supported) and not working at all.
David.Chu.ca
This one (actually my codes) works without align: "Get-WmiObject Win32_LogicalDisk -ComputerName $computer | Select @{Name="Drive"; Expression={$_.Name}}, @{Name="FreeSpace(MB)"; Expression={"{0:N2}" -f ([float]$_.FreeSpace / 1MB)}} | ft -auto", but it does not support align directive.
David.Chu.ca
I'm running PowerShell v2 if that matters. Run "Get-Host" to see what you're running.
xcud
My PS version is 2.0 from Get-Host.
David.Chu.ca