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
}

»crosslinked«

Tagged with:
 

6 Responses to List ALL properties and subproperties of a variable in Powershell

  1. Matt Watson says:

    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.

  2. Matt Watson says:

    @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

    • admin says:

      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

  3. NiTRo says:

    Hi Hugo,

    copy/paste gives ugly thing in my script editor, could you link the ps1 file please ?
    Thanks for your time

  4. […] 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 […]

  5. NiTRo says:

    Hi Hugo, here is my humble contribution : http://vm.lc/8e
    It now handle multiple Array and deserialized objects.
    Thanks again.

Leave a Reply