Get values of all the properties of a PowerShell object
Lets say you want to list all properties of an object but you don’t know all of them by name. Here is comming in help the Get-Member cmdlet. I find it very useful especially when working with new objects that I’m not familiar with.
I’m going to give an example with the list of network adapters listed from the WMI.
So, lets list all of the members.
Get-Wmiobject Win32_NetworkAdapter | where { $_.Speed -ne $null -and $_.MACAddress -ne $null } | Get-Member
TypeName: System.Management.ManagementObject#root\cimv2\Win32_NetworkAdapter
Name MemberType Definition
---- ---------- ----------
PSComputerName AliasProperty PSComputerName = __SERVER
Disable Method System.Management.ManagementBaseObject Disable()
Enable Method System.Management.ManagementBaseObject Enable()
Reset Method System.Management.ManagementBaseObject Reset()
SetPowerState Method System.Management.ManagementBaseObject SetPowerState(System.UInt16 PowerSt...
AdapterType Property string AdapterType {get;set;}
AdapterTypeId Property uint16 AdapterTypeId {get;set;}
AutoSense Property bool AutoSense {get;set;}
Availability Property uint16 Availability {get;set;}
Caption Property string Caption {get;set;}
ConfigManagerErrorCode Property uint32 ConfigManagerErrorCode {get;set;}
ConfigManagerUserConfig Property bool ConfigManagerUserConfig {get;set;}
CreationClassName Property string CreationClassName {get;set;}
Description Property string Description {get;set;}
DeviceID Property string DeviceID {get;set;}
ErrorCleared Property bool ErrorCleared {get;set;}
ErrorDescription Property string ErrorDescription {get;set;}
GUID Property string GUID {get;set;}
Index Property uint32 Index {get;set;}
InstallDate Property string InstallDate {get;set;}
Installed Property bool Installed {get;set;}
InterfaceIndex Property uint32 InterfaceIndex {get;set;}
LastErrorCode Property uint32 LastErrorCode {get;set;}
MACAddress Property string MACAddress {get;set;}
Manufacturer Property string Manufacturer {get;set;}
MaxNumberControlled Property uint32 MaxNumberControlled {get;set;}
MaxSpeed Property uint64 MaxSpeed {get;set;}
Name Property string Name {get;set;}
NetConnectionID Property string NetConnectionID {get;set;}
NetConnectionStatus Property uint16 NetConnectionStatus {get;set;}
NetEnabled Property bool NetEnabled {get;set;}
NetworkAddresses Property string[] NetworkAddresses {get;set;}
PermanentAddress Property string PermanentAddress {get;set;}
PhysicalAdapter Property bool PhysicalAdapter {get;set;}
PNPDeviceID Property string PNPDeviceID {get;set;}
PowerManagementCapabilities Property uint16[] PowerManagementCapabilities {get;set;}
PowerManagementSupported Property bool PowerManagementSupported {get;set;}
ProductName Property string ProductName {get;set;}
ServiceName Property string ServiceName {get;set;}
Speed Property uint64 Speed {get;set;}
Status Property string Status {get;set;}
StatusInfo Property uint16 StatusInfo {get;set;}
SystemCreationClassName Property string SystemCreationClassName {get;set;}
SystemName Property string SystemName {get;set;}
TimeOfLastReset Property string TimeOfLastReset {get;set;}
__CLASS Property string __CLASS {get;set;}
__DERIVATION Property string[] __DERIVATION {get;set;}
__DYNASTY Property string __DYNASTY {get;set;}
__GENUS Property int __GENUS {get;set;}
__NAMESPACE Property string __NAMESPACE {get;set;}
__PATH Property string __PATH {get;set;}
__PROPERTY_COUNT Property int __PROPERTY_COUNT {get;set;}
__RELPATH Property string __RELPATH {get;set;}
__SERVER Property string __SERVER {get;set;}
__SUPERCLASS Property string __SUPERCLASS {get;set;}
PSStatus PropertySet PSStatus {Availability, Name, Status, StatusInfo, DeviceID}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
We see that we have more than what we need – AliasProperty, Method, PropertySet, ScriptMethod. So We need to filter by Type:
Get-Member -Type Property
Also we don’t want to print the special properties in this case – starting with “__” so we add extra filter:
where { $_.Name -notmatch "^_" }
Now we have all member properties filtered and we are ready to display them:
foreach { "$($_.Name): $($obj.$($_.Name))" }
What we did is that we print the name of the property and right after that we print the value of the property. You might be wondering where this $obj came from. It is comming from a previous foreachloop in the pipeline:
foreach { $obj = $_; $_ | Get-Member -Type Property | where { $_.Name -notmatch "^_" } | foreach { "$($_.Name): $($obj.$($_.Name))" } }
In the $obj we are saving the object of which we want to display all of the properties.
I added an extra part in the pipeline so we show only for one object not for all:
Select -First 1
And here is the full example:
Get-Wmiobject Win32_NetworkAdapter | where { $_.Speed -ne $null -and $_.MACAddress -ne $null } | Select -First 1 | foreach { $obj = $_; $_ | Get-Member -Type Property | where { $_.Name -notmatch "^_" } | foreach { "$($_.Name): $($obj.$($_.Name))" } }
AdapterType: Ethernet 802.3
AdapterTypeId: 0
AutoSense:
Availability: 3
Caption: [00000010] Intel 21140-Based PCI Fast Ethernet Adapter (Emulated)
ConfigManagerErrorCode: 0
ConfigManagerUserConfig: False
CreationClassName: Win32_NetworkAdapter
Description: Intel 21140-Based PCI Fast Ethernet Adapter (Emulated)
DeviceID: 10
ErrorCleared:
ErrorDescription:
GUID: {40C678F7-6D89-4246-BD82-DB25396D0D9F}
Index: 10
InstallDate:
Installed: True
InterfaceIndex: 12
LastErrorCode:
MACAddress: 00:15:5D:11:0A:1B
Manufacturer: Intel
MaxNumberControlled: 0
MaxSpeed:
Name: Intel 21140-Based PCI Fast Ethernet Adapter (Emulated)
NetConnectionID: Local Area Connection
NetConnectionStatus: 2
NetEnabled: True
NetworkAddresses:
PermanentAddress:
PhysicalAdapter: True
PNPDeviceID: PCI\VEN_1011&DEV_0009&SUBSYS_21140A00&REV_20\3&267A616A&2&50
PowerManagementCapabilities:
PowerManagementSupported: False
ProductName: Intel 21140-Based PCI Fast Ethernet Adapter (Emulated)
ServiceName: dc21x4vm
Speed: 100000000
Status:
StatusInfo:
SystemCreationClassName: Win32_ComputerSystem
SystemName: PIXEL
TimeOfLastReset: 20161216145758.492981-300
You can use this for all kind of objects. Enjoy!