Dynamically get PSCustomObject property and values

Shorter, more PowerShell'ish option:

$test | Get-Member -MemberType NoteProperty | % Name | %{
    $_ + '=' + $test.$_
}

My variant:

$memberNames = ($test | Get-Member -Type NoteProperty).Name
foreach ($mname in $memberNames) {
    "{0}={1}" -f $mname,$Test."$mname"
}

The only way I could find (so far) is to do something like:

$test = [pscustomobject]@{
    First = "Donald";
    Middle = "Fauntleroy";
    Last = "Duck";
    Age = 80
}

$props = Get-Member -InputObject $test -MemberType NoteProperty

foreach($prop in $props) {
    $propValue = $test | Select-Object -ExpandProperty $prop.Name
    $prop.Name + "=" + $propValue
}

The key is using -ExpandProperty.


Not sure if it is really better, but here's one more variant:

$test.psobject.Members | ? {$_.Membertype -eq "noteproperty"} | 
    %{ $_.Name + '='+  $_.Value }