Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Format Text Out as a Table (Format-Table) in PowerShell

, 20 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Format text out as a table (format-table) in PowerShell

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 works with object or an object collection.

For example, if you have an object collection from your code, you can output as a table.

Get-SPSite  -Limit All | Format-Table –AutoSize
  • AutoSize will be used to format columns depending on 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.)

Example

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

image

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

Example: 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

Example: 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Melick

Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 20 Sep 2013
Article Copyright 2013 by Melick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid