What I’m trying to do is a wmi query for the serial number that only returns the serial number. Using wmic it would look like this;

wmic bios get serialnumber

It would return this;

AJRYTS1

In PS this is what I use;

get-wmiobject -class win32_bios -property serialnumber

This is what is returned;

__GENUS          : 2
__CLASS          : Win32_BIOS
__SUPERCLASS     : 
__DYNASTY        : 
__RELPATH        : 
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         : 
__NAMESPACE      : 
__PATH           : 
SerialNumber     : AJRYTS1
PSComputerName   : 

How can I filter that to just give the serial number as in example one?

6 Spice ups

try:

get-wmiobject -class win32_bios -property serialnumber |fl Serialnumber
2 Spice ups

Format-list. Excellent. You da man. THX

I wish they would put an alias for the class. EG… “32_” or “64_” Maybe something along those lines…

Thanks for the super fast response Gary.

Got a, probably not for you, more complicated one. Multiple queries. Would you like it in a new thread?

I would recommend using Select-Object vs Format-Table as it keeps objects in the pipeline as objects vs formatting into a table which is only good for displaying in console

Get-WMIObject Win32_Bios | Select SerialNumber
3 Spice ups

To get the exact same result as with wmic you have to use ExpandProperty as well

Get-WMIObject -Class Win32_Bios | Select-Object -ExpandProperty SerialNumber
1 Spice up

Just to be thorough, you can also:

(Get-WMIObject -Class Win32_Bios).SerialNumber
2 Spice ups

But you gotta be careful with the syntax Martin shown because that does not work in PowerShell 2 if Get-WMIObject returns more than one object. Which in this case it does not. But If you queried for local users and their names, for example, you’d likely get a collection of objects.

(gwmi win32_userAccount ).Name

With PowerShell 3 this works perfectly fine and returns the names of the users. With PowerShell 2 it returns nothing.

The reason for this is PowerShell 2 you query the Name of the collection itself, not each item (user) in the collection. In PowerShell 3 the engine automatically expands the collection if needed and returns Name of each disk.

(Call me a dinosaur but most of the scripts I write are and have to be Posh 2 compatible :slight_smile: )

Isn’t that the truth? I’ve been writing all 3.0+ higher scripts even though most of our servers don’t support it just to FORCE the issue :slight_smile: We now have “batch job” servers that are all PS 4.0.

Obviously you do not have a ton of Vista stations to support, where updating to PowerShell 3 does not make enough economical sense :slight_smile:

No, just over 500 Windows 2008 R2 servers! All the workstations are Windows 7 so if I have to write something for them I have to suffer with PS 2.0.

Vista? Really?