What Is Going On Here?

by Oct 9, 2013

Frequently, you will have PowerShell retrieve data, and then you pick parts of the information and use it in reports. Like here:

$serial = Get-WmiObject -Class Win32_OperatingSystem | 
  Select-Object -Property SerialNumber

"Serial Number is $serial" 

But the above code will produce a result like this:

When you double-check $serial, it looks ok:

Problem is the column header. Whenever you use Select-Object to just pick one column, remove the header by using -ExpandProperty rather than -Property:

$serial = Get-WmiObject -Class Win32_OperatingSystem | 
  Select-Object -ExpandProperty SerialNumber

"Serial Number is $serial" 

Now, all is fine:

Twitter This Tip! ReTweet this Tip!