Formatting Text Output

by Jan 18, 2016

If you need to return multiple items in a nicely formatted text report, here is a simple trick: get yourself an ordered hash table (supported in PowerShell 3.0 or better), assign the items you want to output, and then convert it into an object. This object can then easily be formatted as text:

$info = [Ordered]@{}

$info.'BIOS Serial' = (Get-WmiObject Win32_BIOS).SerialNumber
$info.'Currently logged-in user' = $env:username
$info.'Date of day' = Get-Date
$info.Remark = 'Some remark'

New-Object PSObject -Property $info | Format-List | Out-String

The result looks like this:

 
BIOS Serial              : 5TQLM32
Currently logged-in user : Tobias
Date of day              : 05.01.2016 11:52:02
Remark                   : Some remark 
 

 

Throughout this month, we'd like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!