As a PowerShell enthusiast, I often work with objects and their properties. One of the most useful cmdlets for manipulating objects is Select-Object. In this tutorial, I’ll show you how to use Select-Object to retrieve only the property values without the property names with some useful examples.
PowerShell Select-Object
Select-Object in PowerShell allows you to select specific properties of an object and create new, custom objects with those properties. By default, when you use Select-Object to select properties, it returns new objects that have only the specified properties, but it includes both the property names and their values.
The -ExpandProperty Parameter
To retrieve only the property values without the property names, we can leverage the -ExpandProperty parameter of Select-Object. This parameter allows us to specify a single property name, and it will return only the value of that property.
Here’s an example:
$computers = Get-ADComputer -Filter *
$computers | Select-Object -ExpandProperty NameIn this case, we retrieve all computer objects from Active Directory using Get-ADComputer and then pipe the results to Select-Object. By using the -ExpandProperty parameter and specifying the “Name” property, we only get the values of the “Name” property, without the property name itself.
Check out PowerShell Select-Object Without Header
Select Multiple Properties
If you need to retrieve the values of multiple properties without the property names, you can use a combination of Select-Object and ForEach-Object. Here’s how:
$users = Get-ADUser -Filter *
$users | Select-Object -Property Name, SamAccountName | ForEach-Object { $_.Name; $_.SamAccountName }In this example, we retrieve all user objects from Active Directory using Get-ADUser. We then use Select-Object to select the “Name” and “SamAccountName” properties. Finally, we pipe the results to ForEach-Object and output the values of the “Name” and “SamAccountName” properties separately.
Measure Command Execution Time
Sometimes, you might want to measure the execution time of a command and retrieve only the value of the “TotalSeconds” property. Here’s an example:
Measure-Command { Get-ChildItem C:\ } | Select-Object -ExpandProperty TotalSecondsIn this case, we use Measure-Command to measure the execution time of the Get-ChildItem command. We then pipe the result to Select-Object and use the -ExpandProperty parameter to retrieve only the value of the “TotalSeconds” property.
Check out PowerShell Select-Object -Unique
Dealing with Single-Property Objects
When working with objects that have only a single property, you can directly access the property value without using Select-Object. For example, let’s say you have a $Computer object with an “IPAddressToString” property:
$Computer.IPAddressToStringThis will return the value of the “IPAddressToString” property directly.
Getting Property Values for Every Object in a List
If you have a list of objects and want to retrieve a specific property value for each object without using a ForEach loop, you can use Select-Object with the -Property parameter. Here’s an example:
$files = Get-ChildItem C:\Temp
$files | Select-Object -Property LengthThis will return the value of the “Length” property for each file object in the $files array.
Conclusion
In this tutorial, I explained various ways to use Select-Object in PowerShell to retrieve only the property values without the property names. By using the -ExpandProperty parameter, combining Select-Object with ForEach-Object, and directly accessing single-property objects, you can achieve this.
I hope you found this tutorial helpful! Feel free to reach out if you have any questions or suggestions.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.