6 small penguins on the ice

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

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:

$os = gwmi win32_operatingsystem
$bios = gwmi win32_bios
$obj = new-object psobject
$obj | add-member noteproperty OSBuild ($os.buildnumber)
$obj | add-member noteproperty BIOSSerial ($bios.serialnumber)
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:

C:\Get-Info | Out-GridView
C:\Get-Info | Export-CSV Info.csv
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.

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish