List ALL properties and subproperties of a variable in Powershell
With one small adjustment, the script in my previous post can be used as a function to list all properties and subproperties of any variable in Windows Powershell. It’s a great way to explore Powershell and create scripts. I used to do the following procedure very often:
$a = Get-Process #For Example
$a
$a[0] | fl *
$a[0].Modules | fl *
etc.
With this function loaded into my profile, this becomes:
$a = Get-Process # For Example
Get-ALLPropertyNames ‘$a’
Here’s the function:
Function Get-ALLPropertyNames
{param([string]$VariableName)
# Function that lists the properties
function Show-Properties
{
Param($BaseName)
If ((Invoke-Expression $BaseName) -ne $null)
{
$Children = (Invoke-Expression $BaseName) | Get-Member -MemberType Property
ForEach ($Child in ($Children | Where {$_.Name -ne “Length” -and $_.Name -notmatch “Dynamic[Property|Type]” -and $_.Name -ne “”}))
{
$NextBase = (“{0}.{1}” -f $BaseName, $Child.Name)
$Invocation = (Invoke-Expression $NextBase)
If ($Invocation)
{
If ($Invocation.GetType().BaseType.Name -eq “Array”)
{
# Recurse through subdir
$NextBase = $NextBase + ‘[0]‘
Show-Properties $NextBase
}
ElseIf ($Child.Definition -notlike “System*”)
{
# Recurse through subdir
Show-Properties $NextBase
}
Else
{
$myObj = “” | Select Name, Value
$myObj.Name = $NextBase
$myObj.Value = $Invocation
$myObj
}
}
Clear-Variable Invocation -ErrorAction SilentlyContinue
Clear-Variable NextBase -ErrorAction SilentlyContinue
}
}
Else
{
Write-Warning “Expand Failed for $BaseName”
}
}# Actual start of script
If ((Invoke-Expression $VariableName).GetType().BaseType.Name -eq “Array”)
{
$VariableName = $VariableName + ‘[0]‘
}
Show-Properties $VariableName
}
No related posts.
6 Responses to List ALL properties and subproperties of a variable in Powershell
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Hey Hugo. This is just what I was looking for. I can’t get this function to work, though and I was wondering if you could help.
I pasted the contents of the function script into a PS1 file and ran it (ie .\Get-AllPropertyNames.ps1). Then, I ran the following sequence once the function was loaded:
$a = Get-Process
Get-AllPropertyNames $a
I attempted variations on the function call: ie. Get-AllPropertyNames ‘$a’
I suspect I’m missing something that’s right in front of me. Any thoughts on this?
BTW, I really dig your blog. You put some fantastic content.
Cheers.
@Matt Watson
I figured it out, Hugo. I was trying to run the function in a PS1 file. I pasted the all the syntax into Powershell and it ran like charm. Again – very very cool function.
Best,
Matt
Matt,
It does work when running from a .ps1 file, but you’ll have to dot-source it (e.g.: . .\script.ps1) to keep the function “alive” in your session.
Thank you very much for dropping a comment.
Hugo
Hi Hugo,
copy/paste gives ugly thing in my script editor, could you link the ps1 file please ?
Thanks for your time
[...] récursivement les propriétés d’un objet Powershell. Nous avons donc pris comme base le fameux script Get-ALLPropertyNames d’Hugo Peeters et l’avons modifié pour qu’il supporte les “array” ainsi que les [...]
Hi Hugo, here is my humble contribution : http://vm.lc/8e
It now handle multiple Array and deserialized objects.
Thanks again.