Weird Text Formatting (and what to do about it)

by Oct 31, 2013

Check out this code and try to find the problem:

$desc = Get-Process -Id $pid | Select-Object -Property Description
"PowerShell process description: $desc" 

This code gets the PowerShell host process and reads the process description, then outputs it in a string. The result looks weird:

This is because the code has selected the entire property Description, and the result is not just the string description, but also the property:

Whenever you select just one property, make sure you use -ExpandProperty instead of -Property. It gets rid of the property column, and the string now looks just fine:

 

Twitter This Tip! ReTweet this Tip!