ChatGPT解决这个技术问题 Extra ChatGPT

How to get an object's property's value by property name?

In PowerShell, how do you get an object's property value by specifying its name (a string)? I want something like the following:

$obj = get-something

# View the object's members:
$obj | gm

# I could retrieve a property by doing so:
write-host $obj.SomeProp

# But for many purposes, I would really want to:
write-host $obj | Get-PropertyByName "SomeProp"

Is there something similar to "Get-PropertyByName" in PowerShell?


a
aquinas

Sure

write-host ($obj | Select -ExpandProperty "SomeProp")

Or for that matter:

$obj."SomeProp"

remember expand: select -expand "SomeProp" if you want the value. select "SomeProp" returns a customobject with a property "SomeProp", and then he's pretty much back at step 1.
Is there a way to use $obj."SomeProp" in write-host?
@TheMuffinMan, yes, it is a generally applicable feature, like write-host $obj."$somepropertyname"
When your string is in a variable, $obj.($propName) also works. (The parentheses are not required, but it looks really weird to me without them.)
If you have the $propName stored in a object for example $Headers.PropertyName then the parentheses are required $obj.($Headers.PropertyName).
S
Stephen Ostermiller

Expanding upon @aquinas:

Get-something | select -ExpandProperty PropertyName

or

Get-something | select -expand PropertyName

or

Get-something | select -exp PropertyName

I made these suggestions for those that might just be looking for a single-line command to obtain some piece of information and wanted to include a real-world example.

In managing Office 365 via PowerShell, here was an example I used to obtain all of the users/groups that had been added to the "BookInPolicy" list:

Get-CalendarProcessing conferenceroom@example.com | Select -expand BookInPolicy

Just using "Select BookInPolicy" was cutting off several members, so thank you for this information!


C
Charlie Joynt

You can get a property by name using the Select-Object cmdlet and specifying the property name(s) that you're interested in. Note that this doesn't simply return the raw value for that property; instead you get something that still behaves like an object.

[PS]> $property = (Get-Process)[0] | Select-Object -Property Name

[PS]> $property

Name
----
armsvc

[PS]> $property.GetType().FullName
System.Management.Automation.PSCustomObject

In order to use the value for that property, you will still need to identify which property you are after, even if there is only one property:

[PS]> $property.Name
armsvc

[PS]> $property -eq "armsvc"
False

[PS]> $property.Name -eq "armsvc"
True

[PS]> $property.Name.GetType().FullName
System.String

As per other answers here, if you want to use a single property within a string, you need to evaluate the expression (put brackets around it) and prefix with a dollar sign ($) to declare the expression dynamically as a variable to be inserted into the string:

[PS]> "The first process in the list is: $($property.Name)"
The first process in the list is: armsvc

Quite correctly, others have answered this question by recommending the -ExpandProperty parameter for the Select-Object cmdlet. This bypasses some of the headache by returning the value of the property specified, but you will want to use different approaches in different scenarios.

-ExpandProperty Specifies a property to select, and indicates that an attempt should be made to expand that property https://technet.microsoft.com/en-us/library/hh849895.aspx

[PS]> (Get-Process)[0] | Select-Object -ExpandProperty Name
armsvc


J
John Slegers

Try this :

$obj = @{
    SomeProp = "Hello"
}

Write-Host "Property Value is $($obj."SomeProp")"

Welcome to StackOverflow, please edit your question and explain why he should try this and why it improves the already existing answers.
I'm conflicted on upvote versus downvote because this answer is IMO the most simple way to safely get a property based on having the string name of the property from an object but also doesn't offer any explanation.
``` $obj = @{ Prop = "Value"; }; $propName = "Prop"'; Write-Host "The value of $propName is $($obj."$propName")" $propName = "NonexistentProp"'; Write-Host "The value of $propName is $($obj."$propName")" ``` will print The value of Prop is Value and then The value of NonexistentProp is
WOW! the markdown documentation is linked in the help for comments but doesn't apply to them. really nice SO devs
D
Duncan Clay

Here is an alternative way to get an object's property value:

write-host $(get-something).SomeProp

Works with propname held in a variable: ` $someprop = "SomeProp"; write-host $(get-something).$someprop`
L
Laurel
$com1 = new-object PSobject                                                         #Task1
$com2 = new-object PSobject                                                         #Task1
$com3 = new-object PSobject                                                         #Task1



$com1 | add-member noteproperty -name user -value jindpal                           #Task2
$com1 | add-member noteproperty -name code -value IT01                              #Task2
$com1 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}       #Task3


$com2 | add-member noteproperty -name user -value singh                             #Task2
$com2 | add-member noteproperty -name code -value IT02                              #Task2
$com2 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}       #Task3


$com3 | add-member noteproperty -name user -value dhanoa                             #Task2
$com3 | add-member noteproperty -name code -value IT03                               #Task2
$com3 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}        #Task3


$arr += $com1, $com2, $com3                                                          #Task4


write-host "windows version of computer1 is: "$com1.ver()                            #Task3
write-host "user name of computer1 is: "$com1.user                                   #Task6
write-host "code of computer1 is: "$com1,code                                        #Task5
write-host "windows version of computer2 is: "$com2.ver()                            #Task3
write-host "user name of computer2 is: "$com2.user                                   #Task6
write-host "windows version of computer3 is: "$com3.ver()                            #Task3
write-host "user name of computer3 is: "$com1.user                                   #Task6
write-host "code of computer3 is: "$com3,code                                        #Task5

read-host

$arr =@("jind",12, "singh") write-host $arr[1] read-host $arr += "reza" write-host $arr[3] read-host write-host $arr[$arr.length-1] read-host $arr = $arr -ne $arr[1] write-host $arr read-host foreach ($i in $arr) {write-host $i}

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now