powershellobject-to-string

how to convert pc serial number to string


First of all I'd like to know if I can use these two instructions

gwmi win32_bios | select serialnumber

gwmi win32_Computersystemproduct | select identifyingnumber

indifferently.

The second question is why if I write

$sn = gwmi win32_bios | select serialnumber | out-string

$sn.gettype() returns me system.object

and

$sn.length returns me 561 even though my serial number is made of 22 chars. Thanks.


Solution

  • By using Out-String, you are converting the output of gwmi win32_bios | select serialnumber to a string and storing it in $sn. So, $sn will now have the following content:

    PS> $sn
    
    serialnumber
    ------------
    xxxxxxx
    

    So, $sn.length is showing you the length of this entire string. If you want to change it only to the serial number:

    PS> $sn = gwmi win32_bios | select -Expand serialnumber | out-string
    PS> $sn
    xxxxxxx    
    PS> $sn.Length
    9
    

    As you can see, my serial number (I removed the original) is only 7 characters wide. But, $sn.length shows 9. There are probably a couple hidden chars after the output. I can see a blank line after the output at the console.

    Coming to the real point, this space is added by Out-String. So, you don't even need that. you can do:

    PS> $sn = gwmi win32_bios | select -Expand serialnumber
    PS> $sn
    XXXXXX
    PS> $sn.Length
    7
    

    $sn is still a string.

    PS> $sn.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object