How to display or process variable output

Hi - new to PS.

code:

$serialnum = get-ciminstance win32_bios | select serialnumber
write-host $serialnum

Output is: @{serialnumber=MGG18960XY7}

Questions:

  1. How do I change output to just get the number portion - MGG18960XY7
  2. Would you please explain why the output is happening like it is.
Thanks.

 

simple.

The output you get is an object, you can see the type by executing below expression.

Get-CimInstance -ClassName Win32_Bios | Get-Member

Srialnumber is property and MGG18960XY7 is the value of that property. If need only the property, you need to either save it to a variable and checrry pick or expand the property to get only the value.

$Bios = Get-CimInstance -ClassName Win32_Bios
Write-Host $Bios.SerialNumber

#Or

$Serialnumber = Get-CimInstance -ClassName Win32_Bios | Select-Object -ExpandProperty SerialNumber
Write-host $Serialnumber

the reason why you got that output is because of string expansion. Write host does a string expansion.

$serialnum = Get-CimInstance -ClassName Win32_Bios | Select-Object -Property serialnumber
"$Serialnum"

I believe and correct me if I’m wrong is you wrapped the Ciminstance into a variable to then call it, to get your answer.

First, you could have just ran the command:

get-ciminstance win32_bios | select serialnumber

But if you are placing it into a variable to call it later then this would have worked as well

$serialnum = get-ciminstance win32_bios | select serialnumber

$serialnum

 

It’s an PSCustomObject with a property. To make $serialnum just a string:

$serialnum = get-ciminstance win32_bios | select -expand serialnumber
write-host $serialnum

ABC1234
  1. How to get the serial number alone: Use the -ExpandProperty along with the Select-object
$serialnum = get-ciminstance win32_bios | select serialnumber -ExpandProperty serialnumber
write-host $serialnum
2. get-ciminstance win32_bios | select -expand serialnumber is having several properties, hence it is showing as @{}. To see the difference use get-member
get-ciminstance win32_bios | select serialnumber -ExpandProperty serialnumber | gm

TypeName: System.String

get-ciminstance win32_bios | select serialnumber

serialnumber 
------------ 
2289-0334-4018-3657-1028-9433-96
get-ciminstance win32_bios | select serialnumber | gm


TypeName: Selected.Microsoft.Management.Infrastructure.CimInstance