Dynamically get PSCustomObject property and values
Asked Answered
N

7

15

I have the following:

$test = [pscustomobject]@{
    First = "Donald";
    Middle = "Fauntleroy";
    Last = "Duck";
    Age = 80
}
$test | Get-Member -MemberType NoteProperty | % {"$($_.Name)="}

which prints:

Age=
First=
Last=
Middle=

I would like to extract the value from each property and have it included as the value for my name value pairs so it would look like:

Age=80
First=Donald
Last=Duck
Middle=Fauntleroy

I am trying to build a string and do not know the property names ahead of time. How do I pull the values to complete my name value pairs?

Nucleoside answered 28/11, 2014 at 20:4 Comment(0)
N
22

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.

Nucleoside answered 28/11, 2014 at 20:59 Comment(2)
Slight improvement to the value lookup: $prop.Name + "=" + $test.($prop.Name)Fonsie
@Fonsie Ironic that I needed this 2 year old thread the same day you posted this! Your way is very succinct and a good answer to the question. I would move it to an answer!Escargot
F
10

Shorter, more PowerShell'ish option:

$test | Get-Member -MemberType NoteProperty | % Name | %{
    $_ + '=' + $test.$_
}
Fonsie answered 4/5, 2016 at 13:56 Comment(1)
$test.($_.Name) also works so you can take out | % NamePrader
P
4

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

$test.psobject.Members | ? {$_.Membertype -eq "noteproperty"} | 
    %{ $_.Name + '='+  $_.Value }
Pasadis answered 11/10, 2016 at 11:49 Comment(0)
D
2

My variant:

$memberNames = ($test | Get-Member -Type NoteProperty).Name
foreach ($mname in $memberNames) {
    "{0}={1}" -f $mname,$Test."$mname"
}
Doriedorin answered 15/6, 2017 at 12:55 Comment(0)
J
1

A shorter way for this old problem. You can use:

$test.PSObject.Properties | %{Write-Output "$($_.Name)=$($_.Value)"}

If you want to access some field programmatically in runtime, by using variable as a field selector, then you can use:

$myProperty = "Age"
$test.PSObject.Properties[$myProperty].Value
Jarboe answered 20/5, 2021 at 21:23 Comment(0)
I
0

I use:

$MyObjectProperties = Get-Member -InputObject $MyObject -MemberType NoteProperty

$MyObjectProperties | % {
    $PropertyName = $_.Name
    $PropertyValue = $MyObject."$PropertyName"
    # ...
}
Irretentive answered 13/11, 2020 at 12:56 Comment(0)
U
0

While this doesn't format as requested it does allow for additional processing and doesn't require additional variables.

$Test.psobject.members `
| Where-Object MemberType -EQ 'NoteProperty' `
| Select-Object name,value

Output:

Name    Value
----    -----
Age     80
First   Donald
Last    Duck
Middle  Fauntleroy

To achieve requested formatting

$Test.psobject.members `
| Where-Object MemberType -EQ 'NoteProperty' `
| Foreach-Object {
    "$($_.name) = $($_.value)"
}

Output:

Age=80
First=Donald
Last=Duck
Middle=Fauntleroy
Unionize answered 12/6, 2023 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.