Examining Object Properties Programmatically

by Oct 23, 2018

Whether you import a CSV list into PowerShell using Import-Csv, or deal with any other type of objects: how can you automatically determine the object properties? Here is a simple approach:

# take any object, and dump a list of its properties Get-Process -Id $pid | Get-Member -MemberType *property | Select-Object -ExpandProperty Name | Sort-Object  

Why would this be useful? There are many use cases. For example, you could identify the names of registry values for a key, and dump the commands for all autostarts:

$RegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" # get actual registry values from path $Values = Get-ItemProperty -Path $RegPath # exclude default properties $default = 'PSChildName','PSDrive','PSParentPath','PSPath','PSProvider' # each value surfaces as object property # get property (value) names $keyNames = $Values | Get-Member -MemberType *Property | Select-Object -ExpandProperty Name | Where-Object { $_ -notin $default } | Sort-Object # dump autostart programs $keyNames | ForEach-Object { $values.$_ } 

Twitter This Tip! ReTweet this Tip!