Q. How can I have my PowerShell script display output in a table?

A. Let's say you want your script to retrieve and display two pieces of information—the BIOS serial number and Windows version—it in a table. The key is to output that information on a custom object:

  1. $os = gwmi win32_operatingsystem
  2. $bios = gwmi win32_bios
  3. $obj = new-object psobject
  4. $obj | add-member noteproperty OSBuild ($os.buildnumber)
  5. $obj | add-member noteproperty BIOSSerial ($bios.serialnumber)
  6. write-output $obj

Doing so opens up a bunch of output possibilities. Let's say all that was contained in a file named C:\Get-Info.ps1:

  1. C:\Get-Info | Out-GridView
  2. C:\Get-Info | Export-CSV Info.csv
  3. C:\Get-Info | Format-Table -autosize

You can do anything with script output if it's in the form of objects.

Do you have a Windows PowerShell question? Find more PowerShell FAQs, articles, and other resources at windowsitpro.com/go/DonJonesPowerShell.