Table of Contents
Getting Object’s First Level Property Value by Name
In PowerShell, we have multiple ways to get an object’s first-level property value by using its name. Before diving into the solutions, it is mandatory to understand how objects are created in PowerShell (if you already know it, you can jump to the solutions). We will use the following object for all the solutions getting an object’s first-level property by its name.
1 2 3 4 5 6 7 8 9 |
$props = $([ordered]@{ firstName="John" lastName="Williamson" age=30 }) $object = New-Object PSObject -Property $props $object |
1 2 3 4 5 |
firstName lastName age --------- -------- --- John Williamson 30 |
First, we used a hash table to create key=value
pairs enclosed within @{}
. The [ordered]
option was used to have properties in the same order they are created; otherwise, the properties would be in a random order whenever you display them. Then, we enclosed the ordered hash table within the parentheses (()
) and prefixed it with a $
to tell PowerShell to evaluate the entire expression inside the ()
as a whole and then assign the returned value to the $props
variable.
After creating the required properties successfully, we used the New-Object
cmdlet to create an object and stored it in the $object
variable. This $object
has all the properties stored in $props
; we specified it using the -Property
parameter. Next, we used the New-Object
cmdlet with the PSObject
class to add the given property to the object as NoteProperty
. A NoteProperty
is created when we specify a particular property which does not exist on that object. Remember, if you do not use the PSObject
class for this specific use case, you will get a non-terminating error generated by the New-Object
cmdlet.
Use Dot Notation
Use Dot notation (.
) to get object property value by name in PowerShell.
1 2 3 4 5 |
$object.firstName $object.lastName $object.age |
1 2 3 4 5 |
John Williamson 30 |
Use Select-Object
Cmdlet
Use the Select-Object
cmdlet to get the object’s one property value by name in PowerShell.
1 2 3 |
$object | Select-Object firstName |
1 2 3 4 5 |
firstName --------- John |
Use the Select-Object
cmdlet to get the object’s multiple property values by names in PowerShell.
1 2 3 |
$object | Select-Object firstName, lastName, age |
1 2 3 4 5 |
firstName lastName age --------- -------- --- John Williamson 30 |
Use the Select-Object
cmdlet with the -ExpandProperty
parameter to get object property value only by name in PowerShell.
1 2 3 |
$object | Select-Object -ExpandProperty firstName |
1 2 3 |
John |
In the above examples, $object
was the object we used to access its properties. We piped the $object
to the Select-Object
cmdlet to get its property value by using its property name(s). Note that the Select-Object
cmdlet is used to select the specified property of the object or set of objects and display an object having a property name and its value. If you want to get property value only, use the -ExpandProperty
parameter with the Select-Object
cmdlet (we used it in the third code snippet).
The -ExpandProperty
parameter of the Select-Object
cmdlet is used to retrieve the value of the specified single property of an object and expand it into an output stream as the new object. Remember, you will get an error stating it can not convert the System.Object[]
to the type System.String
required by the ExpandProperty
parameter if you will try to use -ExpandProperty
to expand multiple properties of $object
because it only works with one property.
We can also use the
Select
alias to get object property value by name in PowerShell. TheSelect
is an alias of theSelect-Object
cmdlet. So, replacingSelect-Object
withSelect
in the above examples will also work.
Use Get-Member
Cmdlet
Use the Get-Member
cmdlet to get the object’s one property value by name in PowerShell.
1 2 3 |
$object | Get-Member -MemberType NoteProperty firstName |
1 2 3 4 5 6 |
TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- firstName NoteProperty string firstName=John |
Use the Get-Member
cmdlet to get the object’s multiple property values by names in PowerShell.
1 2 3 |
$object | Get-Member -MemberType NoteProperty firstName, lastName |
1 2 3 4 5 6 7 |
TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- firstName NoteProperty string firstName=John lastName NoteProperty string lastName=Williamson |
Use the Get-Member
cmdlet with the -ExpandProperty
parameter to get object property value only by name in PowerShell.
1 2 3 4 5 6 |
$strObj = $object | Get-Member -MemberType NoteProperty firstName | Select-Object -ExpandProperty Definition $strObj.Split('=')[1] |
1 2 3 |
John |
In the above examples, we piped the $object
to the Get-Member
cmdlet to retrieve a particular property or properties (separated by a comma); see the first and second code examples. We used the -MemberType
parameter to tell the Get-Member
cmdlet what to retrieve, whether it is a Method
, Property
or NoteProperty
; we mentioned NoteProperty
because we wanted to access the properties that we created.
The third code snippet is similar to the first and second examples, with a few additional things. It also piped the $object
to the Get-Member
cmdlet and set the -MemberType
parameter to NoteProperty
for retrieving the firstName
property. Here, the Get-Member
produced the PSCustomObject
having Name
, MemberType
, and Definition
properties. You use $object | Get-Member -MemberType NoteProperty firstName
to look at them.
We piped this PSCustomObject
to the Select-Object
cmdlet, which selected the Definition
property and expanded it using the -ExpandProperty
parameter. Now, we would have a string type object as string firstName=John
; you can look at it using the $object | Get-Member -MemberType NoteProperty firstName | Select-Object -ExpandProperty Definition
command.
We stored this string object in the $strObj
variable and chained the Split()
method to split the $strObj
using =
as a delimiter. The Split()
method returned an array of substrings having two values: string firstName
and John
. We used the index operator ([1]
) to retrieve the element at index 1
, which was John
.
We can also use the
gm
alias to get specified members in PowerShell. Thegm
is an alias of theGet-Member
cmdlet. So, replacingGet-Member
withgm
in the above examples will also work.
We use the ForEach-Object
cmdlet to iterate over multiple string objects to get values of all properties as follows.
1 2 3 4 5 6 |
$object | gm -MemberType NoteProperty | Select-Object -ExpandProperty Definition | ForEach-Object { $_.Split('=')[1] } |
1 2 3 4 5 |
30 John Williamson |
In the above examples, we statically mentioned the property name(s) to get its value, which is fine if the object has a few properties. But, in real-world problems, an object can have hundreds of properties, and it is hard to remember all of them.
In that case, we write a script that accepts property name(s) as user input and retrieves its value. What approaches can be used for this particular use case? Let’s learn them below. Remember, the script will generate an error if the user will not provide the property name(s).
Use Invoke-Expression
Cmdlet
Use the Invoke-Expression
cmdlet to get the object’s one property value by name in PowerShell.
1 2 3 4 |
$propName = Read-Host "Please enter property name" Invoke-Expression "`$object.$propName" |
1 2 3 4 |
Please enter property name: firstName John |
Use the Invoke-Expression
cmdlet to get the object’s multiple property values by names in PowerShell.
1 2 3 4 |
$propNames = Read-Host "Enter property names (comma-separated)" Invoke-Expression "`$object | Select-Object $propNames" |
1 2 3 4 5 6 |
Enter property names (comma-separated): firstName,age firstName age --------- --- John 30 |
In the first code example, we used the Read-Host
cmdlet to get one property name from the user and stored it in the $propName
variable. Then, we used the Invoke-Expression
cmdlet to evaluate the string "$object.$propName"
as PowerShell expressions. This string expression accessed the given property on the $object
using the value of the $propName
variable. We used a backtick to escape the $
character so that it can be treated as a literal string instead of a variable reference.
The second code fence is similar to the first one, but it got multiple property names from the user, separated by a comma and stored in the $propNames
variable. It also used a backtick to escape the $
character in the string expression, but the string expression was $object | Select-Object $propNames
this time. Here, the Invoke-Expression
cmdlet runs the dynamic command to retrieve particular properties from the $object
we piped to the Select-Object
cmdlet. How? The $propNames
was expanded and used as an argument to the Select-Object
cmdlet, which retrieved the given properties from the $object
.
Use Script Block
Use script block to get the object’s one property value by name in PowerShell.
1 2 3 4 5 |
$propName = Read-Host "Please enter property name" $scriptBlock = { param($object) $object.$propName } & $scriptBlock $object |
1 2 3 4 |
Please enter property name: firstName John |
Use script block to get object’s multiple property values by names in PowerShell.
1 2 3 4 5 6 7 8 |
$propNames = Read-Host "Enter property names (comma-separated but not have space before/after a comma)" $scriptBlock = { param($object, $propNames) $object | Select-Object -Property ($propNames -split ',') } & $scriptBlock $object $propNames |
1 2 3 4 5 6 |
Enter property names (comma-separated but not have space before/after a comma): firstName,age firstName age --------- --- John 30 |
In the first code example, we used the Read-Host
cmdlet to get one property name from the user and stored it in the $propName
variable. Then, we created a scrip block using {}
and stored it in the $scriptBlock
variable. This script block used a param
function to accept the $object
as an argument and returned the value of the $propName
on $object
. Finally, we used the &
operator to invoke the $scriptBlock
by specifying $object
as a parameter.
The second code example is similar to the first one but takes multiple property names from the user that are separated by a comma and stored in the $propNames
variable. Here, in the script block, the param
function took two arguments: $object
and $propNames
and returned the value of $propNames
on $object
. How? We used the Select-Object
with the -Property
parameter to select the specified properties from the object piped to it.
Note that the -split
operator was used to split the $propNames
based on a comma (,
); here, a comma was used as a delimiter. Finally, we invoked the $scriptBlock
using the &
operator and passed in the $object
and $propNames
variables as parameters.
Use Dot Notation with a Variable
Use dot notation with a variable to get the object’s one property value by name in PowerShell.
1 2 3 4 |
$propName = Read-Host "Please enter property name" $object.($propName) |
1 2 3 4 |
Please enter property name: firstName John |
Use dot notation with a variable to get an object’s multiple property values by names in PowerShell.
1 2 3 4 |
$propNames = Read-Host "Please enter property name (comma-separated but not have space before/after a comma)" $propNames -split "," | foreach {$object.($_)} |
1 2 3 4 5 |
Please enter property name (comma-separated but not have space before/after a comma): firstName,age John 30 |
In the first example, we used the Read-Host
cmdlet to get the property name from the users and stored it in the $propName
variable. Then, we used the dot notation with a variable as $object.($propName)
to get the value of the given property name for the specified object.
The second code snippet is similar to the first one but accepts multiple property names (separated by a comma) from the user and stores them in the $propNames
variable. Then, we used the -split
operator to split $propNames
using ,
as a delimiter. After performing the split operation, we got an array of substrings that we piped to the foreach
loop. Then, for each item of the received array of substrings, we used the dot notation with a variable ($_
) to get the value of each property.
The parentheses are optional while using dot notation with a variable.
Further reading:
Getting Object’s Nested Property Value by Name
We have already learned how to create an object using PowerShell in the Getting Object’s First Level Property Value by Name section. As far as the nested properties are concerned, we can create them in two ways:
- Using Nested HashTable
- Using PSCustomObject
We have provided both ways below; you can use any of them; all the provided solutions will work for both approaches to create nested properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$props = $([ordered]@{ firstName="John" lastName="Williamson" age=30 Address = @{ Street = "245 Main Street" City = "Lahore" } }) $object = New-Object PSObject -Property $props $object |
1 2 3 4 5 |
firstName lastName age Address --------- -------- --- ------- John Williamson 30 {City, Street} |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$props = $([ordered]@{ firstName="John" lastName="Williamson" age=30 Address = [pscustomobject]@{ Street = "245 Main Street" City = "Lahore" } }) $object = New-Object PSObject -Property $props $object |
1 2 3 4 5 |
firstName lastName age Address --------- -------- --- ------- John Williamson 30 @{Street=245 Main Street; City=Lahore} |
Did you create an object with nested properties? Then, let’s move towards the solutions.
Use Dot Notation
Use dot notation to get the object’s nested property value by nested property name in PowerShell.
1 2 3 4 |
$object.Address.City $object.Address.Street |
1 2 3 4 |
Lahore 245 Main Street |
Use Select-Object
Cmdlet
Use the Select-Object
cmdlet to get the object’s nested property value by nested property name in PowerShell.
1 2 3 |
$object | Select-Object -ExpandProperty Address | Select-Object {$_.City} |
1 2 3 4 5 |
$_.City ------- Lahore |
Alternatively, we can get the City
property value as follows. This approach best suits if you want to have a customized calculated property name.
1 2 3 4 5 |
$object | Select-Object -ExpandProperty Address | Select-Object @{Name='NestedValue';Expression={$_.City}} |
1 2 3 4 5 |
NestedValue ----------- Lahore |
We explored the Select-Object
and -ExpandProperty
in previous sections. Here, in the first example, the $_.City
was the default calculated property name (created at runtime) whose value we retrieved.
On the other hand, in the second example, we used the Select-Object
cmdlet @{}
syntax to create a custom-calculated property name (applicable to display meaningful property names). Inside the @{}
, we used two variable names, Name
and Expression
, where Name
was the calculated property name. In contrast, the Expression
was set to an expression used to calculate the value of the calculated property name.
In this case, the expression is $_.City
, meaning the value of the NestedValue
property would be the value of the City
property of the current object ($_
).
Use Get-Variable
Cmdlet
Use the Get-Variable
cmdlet to get the object’s nested property value by nested property name in PowerShell.
1 2 3 4 5 6 7 |
$varName = "object.Address" $value = (Get-Variable -Name ($varName.Split('.')[0]) -ValueOnly). ($varName.Split('.')[1]) $value.Street $value.City |
1 2 3 4 |
245 Main Street Lahore |
We initialized the $varName
variable with the "object.Address"
value. This variable represented which property to access from the PowerShell object.
Then, we retrieved the value of a nested property given by $varName
using the Get-Variable
cmdlet and dot notation. The Get-Variable
was used to retrieve the value of the $object
variable by specifying the variable name "object"
as a parameter. Run the command as Get-Variable -Name ($variableName.Split('.')[0])
to have a look at what Get-Variable
returned.
Here, we used the Split()
method to split the "object.Address"
string into an array of substrings using .
as a delimiter. This array of substrings contained two items: "object"
and "Address"
. The "object"
(first substring) was used as a parameter for the Get-Variable
, which returned the $object
variable’s value.
The "Address"
(second substring) was used as a property name to retrieve the nested property of $object
. The dot notation was used to get the nested property of an object; this value was then stored in $value
.
Now, the $value
contained the value of a nested Address
property of $object
, which would be another PowerShell object or a hash table, depending on which approach you used to create nested properties, having the Street
and City
properties.
That’s all about PowerShell get object property value by name.