188

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?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
KFL
  • 17,162
  • 17
  • 65
  • 89

6 Answers6

286

Sure

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

Or for that matter:

$obj."SomeProp"
aquinas
  • 23,318
  • 5
  • 58
  • 81
  • 20
    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. – Frode F. Jan 18 '13 at 19:48
  • 3
    Is there a way to use `$obj."SomeProp"` in `write-host`? – The Muffin Man Jan 10 '16 at 01:12
  • @TheMuffinMan, yes, it is a generally applicable feature, like write-host $obj."$somepropertyname" – Elroy Flynn May 26 '17 at 01:44
  • 8
    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.) – jpmc26 Apr 19 '18 at 02:39
  • 7
    If you have the `$propName` stored in a object for example `$Headers.PropertyName` then the parentheses are required `$obj.($Headers.PropertyName)`. – Jonas Lomholdt Aug 09 '18 at 13:25
  • 1
    I personally like: **write-host "$($obj.SomeProp)"** –  Mar 01 '19 at 14:28
  • Can I do that for multiple properties in one line? – tolache Oct 26 '22 at 08:10
70

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!

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
JustaDaKaje
  • 800
  • 5
  • 5
38

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 <String>

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

Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46
17

Try this :

$obj = @{
    SomeProp = "Hello"
}

Write-Host "Property Value is $($obj."SomeProp")"
John Slegers
  • 45,213
  • 22
  • 199
  • 169
rainman-63
  • 171
  • 1
  • 3
  • 1
    Welcome to StackOverflow, please edit your question and explain *why* he should try this and *why* it improves the already existing answers. – T3 H40 Jan 15 '16 at 14:41
  • 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. – Troy Jul 30 '20 at 02:54
  • ``` $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 ` – Troy Jul 30 '20 at 03:01
  • WOW! the markdown documentation is linked in the help for comments but doesn't apply to them. really nice SO devs – Troy Jul 30 '20 at 03:02
8

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

write-host $(get-something).SomeProp
Duncan Clay
  • 81
  • 1
  • 2
  • Works with propname held in a variable: ` $someprop = "SomeProp"; write-host $(get-something).$someprop` – xorcus Dec 28 '21 at 16:41
0
$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
Laurel
  • 5,965
  • 14
  • 31
  • 57
dhanoa
  • 1
  • $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} – dhanoa Oct 12 '16 at 23:04