Friday, September 20, 2013

Format Text Out as a Table (Format-Table) in Power Shell

Format-Table in a powerful command that you can use to format your output. output can be a console or a file out. Format-Table working with object or a object collection.

For an example If you have a object collection from your code you can out put as a table.

Get-SPSite  -Limit All | Format-Table –AutoSize

-AutoSize will be use to format columns with depending of screen size. But if you are working with long width tables you better use Out-String -Width 4000 with output pipe. (4000 is a character count in horizontal, You can mention any suitable value )


Ex:-



Get-SPSite  -Limit All| Format-Table –AutoSize | Out-String -Width 4000

image


You can use Format-Table with a single object as well.


Ex:- With all columns



 
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "saman"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 23423
 
$objAverage | Format-Table –AutoSize

image


Ex:- With mentioned columns



$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "saman"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 23423
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 123
 
$objAverage | Format-Table Col1,Col2 –AutoSize

image


You can also create object collection, add objects and format it as a table.



$table = @()
 
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "aa"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 111
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 222
$table += $objAverage
 
$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Col1 -value "bb"
$objAverage | Add-Member -type NoteProperty -Name Col2 -Value 333
$objAverage | Add-Member -type NoteProperty -Name Col3 -Value 444
$table += $objAverage
 
$table | Format-Table –AutoSize


image

Post a Comment