Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Properties with Values

Posted on July 4, 2011

One of my nuisance issues when using WMI with Windows PowerShell, is that when looking at all properties I have to wade though many that have no value. I'd prefer to only view properties that have a populated value. Here's one way.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Every WMI object has a property called Properties that is a collection of just what you would expect, properties.

[cc lang="PowerShell"]
PS S:\> gwmi win32_computersystem | Select Properties

Properties
----------
{AdminPasswordStatus, AutomaticManagedPagefile, AutomaticResetBootOption, AutomaticResetCapability...}
[/cc]

Because this is a collection, we can use Select-Object's ExpandProperty parameter.

[cc lang="PowerShell"]
PS S:\> gwmi win32_computersystem | Select -ExpandProperty Properties | select -first 2

Name : AdminPasswordStatus
Value : 0
Type : UInt16
IsLocal : True
IsArray : False
Origin : Win32_ComputerSystem
Qualifiers : {CIMTYPE}

Name : AutomaticManagedPagefile
Value : False
Type : Boolean
IsLocal : True
IsArray : False
Origin : Win32_ComputerSystem
Qualifiers : {CIMTYPE}
[/cc]

The object gives me the property name and any value. This means I can construct a PowerShell expression like this:

[cc lang="PowerShell"]
PS S:\> gwmi win32_computersystem | Select -ExpandProperty Properties | where {$_.value} | select Name,Value

Name Value
---- -----
AutomaticResetBootOption True
AutomaticResetCapability True
BootOptionOnLimit 3
BootOptionOnWatchDog 3
BootROMSupported True
BootupState Normal boot
Caption SERENITY
ChassisBootupState 3
CreationClassName Win32_ComputerSystem
CurrentTimeZone -240
DaylightInEffect True
Description AT/AT COMPATIBLE
DNSHostName SERENITY
Domain WORKGROUP
EnableDaylightSavingsTime True
FrontPanelResetStatus 3
KeyboardPasswordStatus 3
Manufacturer TOSHIBA
Model Qosmio X505
Name SERENITY
NetworkServerModeEnabled True
NumberOfLogicalProcessors 8
NumberOfProcessors 1
OEMStringArray ... , Bd-BtD7SW5M3i, bM5oVNHGZz+S9, yGkIbhav0zUFP}
PauseAfterReset 3932100000
PCSystemType 2
PowerSupplyState 3
PrimaryOwnerName Prof. Powershell
ResetCapability 1
ResetCount -1
ResetLimit -1
Roles {LM_Workstation, LM_Server, SQLServer, NT}
Status OK
SystemType x64-based PC
ThermalState 3
TotalPhysicalMemory 8577855488
UserName SERENITY\Jeff
WakeUpType 6
Workgroup WORKGROUP
[/cc]

All I've done is pipe the collection of properties to Where-Object which is filtering out any objects that do not have a value. This is the type of thing where a scriptblock or function would come in handy.

[cc lang="PowerShell"]
PS S:\> $wmi={Param([string]$class) gwmi $class | Select -ExpandProperty Properties | where {$_.value} | Select Name,Value}
PS S:\> &$wmi win32_bios

Name Value
---- -----
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 20101210000000.000000+000
SerialNumber Z9131790W
SMBIOSBIOSVersion V2.90
SMBIOSMajorVersion 2
SMBIOSMinorVersion 6
SMBIOSPresent True
SoftwareElementID Ver 1.00PARTTBL
SoftwareElementState 3
Status OK
Version TOSQCI - 6040000
[/cc]

I'll let you play with this and add support for remote computers or alternate credentials.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d