PowerShell Select-Object -First [With Examples]

In this tutorial, I will explain how to work with the -First parameter of the PowerShell Select-Object cmdlet with some examples.

PowerShell Select-Object -First

The Select-Object cmdlet in PowerShell is used to select specific properties of an object or set of objects. It’s incredibly versatile, allowing you to filter and format data efficiently. One of its key features is the ability to select the first few objects from a collection using the -First parameter.

Syntax of PowerShell Select-Object -First

Here’s the basic syntax for Select-Object with the -First parameter:

Select-Object -First <Number>
  • -First: Specifies the number of objects to select from the beginning of the input.

The -First parameter is particularly useful when you need to quickly retrieve a subset of data from a larger dataset. For instance, if you have a list of users and you want to get the first five entries, Select-Object -First can accomplish this efficiently.

PowerShell Select-Object -First Examples

Now, let me show you some real examples of how Select-Object -First can be used in different scenarios.

Example 1: Select the First 5 Users

Imagine you have a list of users in your organization, and you want to get the first five entries. Here’s how you can do it:

Get-ADUser -Filter * | Select-Object -First 5

This command retrieves all Active Directory users and then selects the first five users from the list. This can be particularly useful for auditing or reporting purposes.

Example 2: Retrieve the First 3 Files in a Directory

Suppose you need the first three files from a directory. The following command will help you achieve this:

Get-ChildItem -Path "C:\MyFolder" | Select-Object -First 3

This command lists all files in the specified directory and selects the first three files. It’s a quick way to sample files from a directory.

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

PowerShell Select-Object -First

Check out PowerShell Select-Object Without Header

Example 3: Fetch the First 10 Processes

If you’re monitoring system performance and want to get the first ten processes, use the below PowerShell script with Select-Object -First parameter.

Get-Process | Select-Object -First 10

This command lists all running processes and selects the first ten. It’s useful for getting a snapshot of the system’s state.

Example 4: Combine with Other Parameters

You can combine -First with other parameters like -Property to select specific properties of the first few objects. For example:

Get-Process | Select-Object -First 5 -Property Name, CPU

This command selects the first five processes and displays only their Name and CPU properties.

Here is the exact output in the screenshot below:

PowerShell Select-Object -First Examples

Read PowerShell Copy-Item

Example 5: Using with Where-Object

You can also use Select-Object -First in conjunction with Where-Object to filter data before selecting the first few objects. For example:

Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object -First 3

This command filters processes with a CPU usage greater than 100 and then selects the first three.

Here is the exact output in the screenshot below:

powershell select-object first

Conclusion

In this tutorial, I explained how to use Select-Object -First in PowerShell. I have also provided a few real examples of PowerShell Select-Object -First parameter.

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.