PowerShell Select-Object Made Simple: A Beginner’s Guide

Published:20 March 2023 - 5 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

PowerShell is a powerful tool for managing data, but also typically provides too much information at once. As you know, too much information can be overwhelming and difficult to parse through. Not to worry, though. The PowerShell Select-Object cmdlet will come in handy.

In this tutorial, you will learn the basics of the Select-Object cmdlet to narrow down or filter results in many ways.

Read on and start painless data management!

Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, be sure you have a system with PowerShell installed. This tutorial uses Windows 10 with PowerShell 7, but any modern version should also work.

What is PowerShell Select-Object cmdlet?

The Select-Object cmdlet simplifies data management by allowing you to select specific properties from an object or dataset. At the same time, you can rename properties or create calculated properties based on existing ones.

The basic syntax of Select-Object is as follows where:

ParameterFunction
-InputObjectSpecifies the object or data set you want to work with, which can be an object, an array of objects, or an output of another cmdlet.
PropertySpecifies the property or properties to select from the InputObject parameter. You can specify multiple properties separated by commas.
Optional ParametersAllows you to refine your selection, such as selecting a certain number of objects or skipping objects.
Select-Object [-InputObject] [Property] [Optional Parameters]

Selecting Objects by Properties

Since more than a short introduction is needed to satisfy your desire to learn about the Select-Object cmdlet, why not start with one of its common use cases? Kick off this tutorial by selecting a specific property from an object with the Select-Object cmdlet.

Suppose you have a list of processes running on your computer, but you only wish to see the names of each process. Specifying the Name property to the Select-Object cmdlet will do the trick.

Open PowerShell and run the below Get-Process command to retrieve and print a list of all processes only (Select-Object) by Name.

Get-Process | Select-Object Name
Selecting objects by property
Selecting objects by property

Besides running processes, the Select-Object cmdlet lets you work on a CSV file. Suppose you have a CSV file with many columns but only need a few of them. In that case, let the Select-Object cmdlet choose only the relevant columns so you can manipulate and analyze the data.

Now, run the following command to select multiple properties (Select-Object): Name and CPU (separated by commas). This feature helps when working with large data sets where you only need some properties of each object.

In addition, this command also sorts the list of objects (Sort-Object) in -Descending order with the CPU property as the reference.

Get-Process | Select-Object Name, CPU | Sort-Object -Descending CPU

Below, you can see the processes listed, starting from the most resource-consuming one, Google Chrome (if you have one installed).

Selecting multiple properties
Selecting multiple properties

Creating a Calculated Property

When looking for properties that better fit your needs, consider creating a calculated property. Why? Calculated properties allow you to manipulate and transform data, combine existing properties, perform mathematical operations, or format data in a specific way.

To create a calculated property, you must specify a new property name and an expression that defines how to calculate the property value:

Run the command below to perform the following:

  • Retrieve a list of running processes (Get-Process), selecting only (Select-Object) the Name and CPU properties.
  • Create a new calculated property with the Name Memory (MB), which calculates the memory usage of each process in megabytes. The memory usage is calculated using an Expression that divides the WorkingSet property by 1MB. Any valid PowerShell expression works in creating a calculated property.
  • Prints all retrieved running processes in -Descending order by the calculated Memory (MB) property.
Get-Process | Select-Object Name, CPU, @{Name="Memory (MB)";Expression={$_.WorkingSet/1MB}} | Sort-Object -Descending "Memory (MB)"
Creating a calculated property
Creating a calculated property

Excluding a Specified Property

In real life, the results of a Select-Object command may contain sensitive information, such as usernames and passwords. How to avoid this risk?

To prevent sensitive data from being displayed in the output, append the -ExcludeProperty parameter to exclude a specified property from the output.

Run the following command to perform the following tasks:

  • Retrieve a list of all the files and folders (Get-ChildItem) in the C:\\ directory recursively (-Recurse).
  • Pipe the output to the Select-Object cmdlet to select the Name, Length, and LastWriteTime properties.
  • Exclude (-ExcludeProperty) the FullName property from the output. Doing so prevents the full paths from being displayed in the output, which may contain confidential information.
Get-ChildItem -Path C:\ -Recurse | Select-Object Name, Length, LastWriteTime -ExcludeProperty FullName
Excluding a specified property
Excluding a specified property

Skipping the First or Last Number of Objects

When working with massive datasets, finding the data you need can be like searching for a diamond in a coal mine. But why waste time sifting through irrelevant data?

In PowerShell, the Select-Object cmdlet provides two parameters that let you exclude the first (-Skip) and last (-SkipLast) number of objects in the output, respectively.

To see how skipping several objects from an output works:

Run the below command to get all running processes (Get-Process) but -Skip the first 100 objects returned.

Note that the number of objects skipped is based on integer values (positive and whole numbers without decimal points), which in this case is 100.

Get-Process | Select-Object -Skip 100
Skipping the first 100 objects
Skipping the first 100 objects

Now, run the following command to get a list of the working directory’s contents (Get-ChildItem) but skip the last (-SkipLast) 100 objects returned.

Get-ChildItem | Select-Object -SkipLast 100
Skipping the last 100 objects
Skipping the last 100 objects

Selecting the First or Last Number of Objects

Besides skipping the first and last number of objects, you may also be interested in selecting the first and last number of objects instead. The Select-Object cmdlet supports this feat with the First and Last parameters that let you skip everything else to save time and resources.

Run the following command to get all processes but only select (Select-Object) the -First 10 objects returned.

Get-Process | Select-Object -First 10
Selecting the first 10 objects returned
Selecting the first 10 objects returned

Now, run the command below to get all contents of the working directory (Get-ChildItem) but only select (Select-Object) the -Last 10 objects returned.

Get-ChildItem | Select-Object -Last 10
Selecting the last 10 objects returned
Selecting the last 10 objects returned

Selecting Unique Objects Exclusively

As you work with larger datasets, you will encounter duplicate values you may want to exclude from the output, which is typical. But the good thing with the Select-Object cmdlet is that you do not have to exclude duplicate values manually.

Appending the -Unique parameter allows you to select only the unique objects from the output, streamlining your analysis and improving your results’ accuracy.

Run the command below to select (Select-Object), and return the -First 2 objects.

"1","1","2","3" | Select-Object -First 2

Below, you can see two returned objects regardless if the other is a duplicate value.

Selecting and returning objects with duplicate values
Selecting and returning objects with duplicate values

Lastly, run the following command to return only one -Unique value for each of the -First 2 objects selected.

"1","1","2","3" | Select-Object -First 2 -Unique

This time, you can see only one returned from the first two objects selected since the other is just a duplicate value. As you realize, this output proves that the -Unique parameter is handy for quickly discarding any duplicate objects in your data.

Selecting unique objects exclusively
Selecting unique objects exclusively

Conclusion

PowerShell tends to produce an overload of information. But in this tutorial, you learned how the PowerShell Select-Object cmdlet helps streamline your data to see only the needed information.

You have seen how different parameters allow you to select and exclude data, especially duplicated values from an output. With the Select-Object cmdlet, managing large amounts of data does not have to be a daunting task.

At this point, you can quickly refine your results to be better suited for analysis and troubleshooting. But why not learn how to format your calculated properties next to improve readability and usability?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!