PowerShell is a data-type driven scripting language, meaning that it supports almost all data types, such as integers, variables, floats, or arrays. More specifically, an array is a data type that stores multiple data types, whether it is a string, variable, or integer. The values stored in an array are stored at a specific index. The first value is stored at the “0” index, the second at the “1” index, and so on. These values can then be selected and called with their specific index number.
This article will cover a detailed procedure to resolve the mentioned query.
How to Select the Values of One Property on all Objects of an Array in PowerShell?
These approaches can be utilized to select the values of one property in an array:
Method 1: Use the “Select-Object” Method to Select all Objects of an Array with the Same Property Values
The “Select-Object” cmdlet is used to select the property values on all objects of an array defined by a user. It is specifically designed to select the specific values defined by a user.
Example
For instance, check the below example code:
[pscustomobject]@{Name='Johnny';Gender='Male';Age='27'}
[pscustomobject]@{Name='Alice';Gender='Female';Age='23'}
[pscustomobject]@{Name='Alex';Gender='Female';Age='28'}
)
According to the above code:
- First of all, create an array and assign it to a “$Employees” variable.
- Inside an array, create three objects. Each object contains three properties which are “Name”, “Gender”, and “Age”.
- Assign required values to the defined properties:
Now, let’s select the values of one property on all objects of an array using the “Select-Object” cmdlet:
Here:
- Add the array assigned variable “$Employees” and then use the “Pipeline |” to pass the array output to the “Select-Object” cmdlet.
- After that, specify the “-ExpandProperty” parameter and assign the “Gender” property to output the values stored in it:
The “Gender” property values on all objects of an array have been displayed in the output.
Method 2: Use the “$array.Property” Method to Select all Objects of an Array with the Same Property Values
Using the “$array.Property” method is the easiest and simplest method to select the values of one property. It concatenates the property with itself to the name to get its values.
Example
In this example, we will access the value of the “Name” property of the “$Employees” array:
Method 3: Use the “%{$_.Property}” Method to Select all Objects of an Array with the Same Property Values
Another method to select and get the values of one property is the “%{$_.Property}” cmdlet. This method is also the easiest one and quite similar to the “$array.Property” cmdlet.
Example
For instance, overview the given example:
Here:
- First of all, specify the variable and then use the “pipeline |” to send its output to the next command “%{$_.Age}” as an input.
- “%{$_.Age}” will then select the values assigned to the “Age” property:
The values of one property across all objects have been selected successfully.
Conclusion
In PowerShell the values of a property of an array in the object can be selected using various methods. These methods include “Select-Object”, “$array.Property”, or “%{$_.Property}”. All three methods display the values of the property across all objects in the array. This post has elaborated a detailed procedure to resolve the mentioned query.