3

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.

Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98

4 Answers4

2

Looks like those two wmi properties give the same result on my machine. I'm guessing that they pull from the same place.

In terms of the results of GetType, I get this:

IsPublic IsSerial Name                                     BaseType                                                                                                                    
-------- -------- ----                                     --------                                                                                                                    
True     True     String                                   System.Object

Which means that $sn is a String, which is derived from System.Object.

Oh yeah...the last part. $sn is not just the serial number. It's The headers, the formatting, the spaces, and all of the properties of the result of the GetType() function.

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
2

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
ravikanth
  • 24,922
  • 4
  • 60
  • 60
1

if you do a get-member on the the output of gwmi win32_bios | select serialnumber you'll see that it in fact has the following properties, like any object in .NET.

typeName: Selected.System.Management.ManagementObject

Name         MemberType   Definition                                
----         ----------   ----------                                
Equals       Method       bool Equals(System.Object obj)            
GetHashCode  Method       int GetHashCode()                         
GetType      Method       type GetType()                            
ToString     Method       string ToString()                         
serialnumber NoteProperty System.String serialnumber=N1B85 T10 55757

If you want the serial number, you need to do the following:

$sn = gwmi win32_bios | select serialnumber
$sn.serialnumber

That way you're selecting the contents of the serialnumber property of the serialnumber object.

Couradical
  • 175
  • 1
  • 7
1

Or you can just do this:

$sn = (gwmi win32_bios).serialnumber 
mjolinor
  • 66,130
  • 7
  • 114
  • 135