Select WMI

I’ve been helping out on some WMI and PowerShell issues in the forums at ScriptingAnswers.com. As I was working on a problem I ended up taking a slight detour to address an issue that has always bugged me. When I run a command like this:

PowerShell wants to return system classes as well.

__GENUS          : 2

__CLASS          : Win32_UserAccount

__SUPERCLASS     :

__DYNASTY        :

__RELPATH        :

__PROPERTY_COUNT : 3

__DERIVATION     : {}

__SERVER         :

__NAMESPACE      :

__PATH           :

Description      :

Disabled         : False

Name             : Jeff

The work around has been to pipe the original expression to Select-Object and re-selecting the properties I want. This seems like an unnecessary step. Now, depending on the class I could simple return all WMI objects and then use Select-Object. But I should be able to take advantage of early filtering and use Get-WmiObject as it was intended. So I wrote a function called Select-WMI that will take any WMI-looking object and return the non system properties.

The function has complete help and examples. I assumed you would run it as part of a pipeline. Now I can more easily get the information I’m after.

PS C:\Scripts> get-wmiobject -query “Select Name,Description,Disabled from Win32_UserAccount” | Select-WMI

Description                                  Disabled Name
———–                                  ——– —-
Built-in account for ad…                      False Administrator
False bob

Built-in account for gu…                       True Guest
False Jeff
False sql
VMware User                                     False __vmware_user__

The function will also work to bypass PowerShell’s default formatting for WMI objects, making it easier to see all the properties.

PS C:\Scripts> gwmi win32_bios | select-wmi

BiosCharacteristics   : {4, 7, 8, 9…}

BIOSVersion           : {TOSQCI – 6040000, Ver 1.00PARTTBL}

BuildNumber           :

Caption               : Ver 1.00PARTTBL

CodeSet               :

CurrentLanguage       :

Description           : Ver 1.00PARTTBL

IdentificationCode    :

InstallableLanguages  :

InstallDate           :

LanguageEdition       :

ListOfLanguages       :

Manufacturer          : TOSHIBA

Name                  : Ver 1.00PARTTBL

OtherTargetOS         :

PrimaryBIOS           : True

ReleaseDate           : 20100223000000.000000+000

SerialNumber          : Z9131790W

SMBIOSBIOSVersion     : V2.20
SMBIOSMajorVersion    : 2

SMBIOSMinorVersion    : 6

SMBIOSPresent         : True

SoftwareElementID     : Ver 1.00PARTTBL

SoftwareElementState  : 3

Status                : OK

TargetOperatingSystem : 0

Version               : TOSQCI – 6040000

But I took this one step further. There are times when all I want are properties that have a value. So Select-WMI has a –Populated parameter so that only populated properties are displayed.

PS C:\Scripts> gwmi win32_bios | select-wmi -Populated

BiosCharacteristics   : {4, 7, 8, 9…}

BIOSVersion           : {TOSQCI – 6040000, Ver 1.00PARTTBL}

Caption               : Ver 1.00PARTTBL

Description           : Ver 1.00PARTTBL

Manufacturer          : TOSHIBA

Name                  : Ver 1.00PARTTBL

PrimaryBIOS           : True

ReleaseDate           : 20100223000000.000000+000

SerialNumber          : Z9131790W

SMBIOSBIOSVersion     : V2.20
SMBIOSMajorVersion    : 2

SMBIOSMinorVersion    : 6

SMBIOSPresent         : True

SoftwareElementID     : Ver 1.00PARTTBL

SoftwareElementState  : 3

Status                : OK

TargetOperatingSystem : 0

Version               : TOSQCI – 6040000

Do you think that’s helpful? You can download the script here.