PowerShell Select-Object Value Only [With Examples]

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 Name

In 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 TotalSeconds

In 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.IPAddressToString

This 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 Length

This 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.