PowerShell Get Object Property Value by Name

PowerShell get object property value by name

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.

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.

Use Select-Object Cmdlet

Use the Select-Object cmdlet to get the object’s one property value by name in PowerShell.

Use the Select-Object cmdlet to get the object’s multiple property values by names in PowerShell.

Use the Select-Object cmdlet with the -ExpandProperty parameter to get object property value only by name in PowerShell.

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-Objectcmdlet 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. The Select is an alias of the Select-Object cmdlet. So, replacing Select-Object with Select 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.

Use the Get-Member cmdlet to get the object’s multiple property values by names in PowerShell.

Use the Get-Member cmdlet with the -ExpandProperty parameter to get object property value only by name in PowerShell.

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. The gm is an alias of the Get-Member cmdlet. So, replacing Get-Member with gm 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.

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.

Use the Invoke-Expression cmdlet to get the object’s multiple property values by names in PowerShell.

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.

Use script block to get object’s multiple property values by names in PowerShell.

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.

Use dot notation with a variable to get an object’s multiple property values by names in PowerShell.

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.

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:

  1. Using Nested HashTable
  2. 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.

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.

Use Select-Object Cmdlet

Use the Select-Object cmdlet to get the object’s nested property value by nested property name in PowerShell.

Alternatively, we can get the City property value as follows. This approach best suits if you want to have a customized calculated property name.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *