This blog will cover a guide about the “Select-Object” and the “-ExpandProperty” cmdlet.
How to Use/Utilize the PowerShell Expand Property for Select-Object?
The cmdlet “Select-Object” selects the objects from the collection of objects. However, the “-ExpandProperty” parameter gets the details of the specific selected property.
Example 1: Display the Selected Properties of an Object Using the “Select-Object” Cmdlet
This example will show only the selected values from an array:
[pscustomobject]@{Pet='Cat';Name='Fairy';Color='White';}
[pscustomobject]@{Pet='Dog';Name='Jimmy';COlor='Black';}
[pscustomobject]@{Pet='Horse';Name='Kim';Color='Brown';}
)
According to the above code:
- First, initialize an array of custom objects and assign it to the “$Pets” variable.
- Inside the array, create three objects and mention the three properties in each object.
- Lastly, define the values defined by the user:
Now, let’s select and display the values assigned to the name property:
In the above-stated code:
- First, add the “$Pets” variable, add the “|” pipeline and then specify the “Select-Object” cmdlet.
- After that, add the “-ExpandProperty” parameter and assign the “Name” value.
- It will display the names of all the objects assigned to them:
Example 2: Display the Last Five Running Process in PowerShell
This example will show the five last running processes using the “Select-Object” cmdlet and “-ExpandProperty” parameter”:
According to the above code:
- First, add the “Get-Process” cmdlet to get the list of processes and then specify the “|” pipeline to transfer the output of the previous cmdlet to the next cmdlet.
- Then, add the “Select-Object” cmdlet, followed by the “-ExpandProperty” parameter, and assign it to the name.
- Lastly, define the “-Last” parameter and assign the value “5” to it:
Example 3: Display the List of First Five Services in PowerShell
This example will show the services using the “Select-Object” cmdlet and “-ExpandProperty” parameter:
That was all about using the PowerShell Expand property for selecting objects.
Conclusion
The objects in PowerShell can be selected with the aid of the “Select-Object” cmdlet. However, the details of the specific objects can be retrieved by adding the “-ExpandProperty” parameter along with the “Select-Object” cmdlet. This blog has elaborated on PowerShell expand property for “Select-Object”.