How to display or process variable output

Welcome Forums General PowerShell Q&A How to display or process variable output

This topic contains 4 replies, has 5 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #160604

    Participant
    Topics: 1
    Replies: 0
    Points: -49
    Rank: Member

    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.

     

  • #160611

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    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"
    
  • #161052

    Participant
    Topics: 2
    Replies: 12
    Points: 74
    Rank: Member

    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

     

  • #161060
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    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
    
  • #161369

    Participant
    Topics: 0
    Replies: 13
    Points: 43
    Helping Hand
    Rank: Member
    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

The topic ‘How to display or process variable output’ is closed to new replies.