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

Explore the Select-Object cmdlet in PowerShell for efficient data filtering and selection. Learn advanced techniques and practical applications

Parveen Singh

8 Mins Read

September 5, 2024

Table of Content

Twitter
LinkedIn
Reddit

PowerShell is a powerful scripting language and command-line shell designed especially for system administration. One of its most versatile and widely-used cmdlets is Select-Object. This cmdlet is essential for filtering and selecting specific properties from objects, making it invaluable for managing data in scripts and automation tasks. In this blog post, you’ll dive deep into Select-Object, exploring its various features and practical applications.

Understanding Select-Object

The Select-Object cmdlet allows you to select specific properties of an object or set of objects. This can be particularly useful when dealing with large datasets or when you only need a subset of information from an object.

Basic Syntax:

Select-Object
      [-InputObject <PSObject>]
      [[-Property] <Object[]>]
      [-ExcludeProperty <String[]>]
      [-ExpandProperty <String>]
      [-Unique]
      [-CaseInsensitive]
      [-Last <Int32>]
      [-First <Int32>]
      [-Skip <Int32>]
      [-Wait]
      [<CommonParameters>]

Let’s walk through some examples to see how Select-Object can be used in various scenarios.

  • Start by opening your PowerShell console. You can do this by searching for PowerShell in your start menu and clicking on it.
  • Run the following command to get a list of running processes:
    Get-Process
    

    This command gives you a comprehensive list of all running processes.

Selecting Specific Properties

To select specific properties from an object, you can use the -Property parameter. This is helpful when you want to narrow down the data to only the relevant fields.

  • Now, use Select-Object to narrow down the data to just the Name, Id, and CPU properties.
    Get-Process | Select-Object -Property Name, Id, CPU
    

    The | character between Get-Process and Select-Object is the “pipe”. It passes the output from Get-Process as the input for Select-Object .

Excluding Properties

Sometimes, you might want to exclude certain properties from the output. The -ExcludeProperty parameter comes in handy in such cases.

  • Use the following command to print the Name and peak metrics of a process using the Peak* wildcard:
    Get-Process | Select-Object -Property Name, Peak* -First 1
    

    The -First 1 property will limit the output to the first process.

  • Now, use the following command to exclude the properties PeakPagedMemorySize and PeakPagedMemorySize64 using the -ExcludeProperty with the *Paged* wildcard:
    Get-Process | Select-Object -Property Name, Peak* -ExcludeProperty *Paged* -First 1
    

    This will exclude all the properties with Paged in the name.

Expanding Properties

  • The -ExpandProperty parameter allows you to expand a property that is an object in itself. This is useful when dealing with nested objects. Run the following command:
    Get-Service -Name "EventLog" | Select-Object -ExpandProperty DependentServices
    

    This command retrieves the DependentServices property of the EventLog service and expands it, showing the details of the dependent services.

Removing Duplicates

  • If you need to remove duplicate entries from your output, use the -Unique parameter. Run the following PowerShell command and observe the output:
    Get-EventLog -LogName System | Select-Object Source -Unique
    

    This command retrieves the unique sources of events in the System event log.

Selecting the First or Last N Objects

  • You can select the first or last N objects from the output using the -First and -Last parameters. Run the following commands and observe the output:
    Get-Process | Select-Object -First 5 Name, CPU
    Get-Process | Select-Object -Last 5 Name, CPU
    

    These commands retrieve the first and last 5 processes, respectively, selecting the Name and CPU properties.

Skipping Objects

  • To skip a specified number of objects and select the rest, use the -Skip parameter. Run the following to skip the first 10 processes:
    Get-Process | Select-Object -Skip 10 Name, CPU
    

Practical Examples

  • Listing Top CPU-Consuming Processes:
    Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU
    

    This command sorts processes by CPU usage in descending order and selects the top 5.

  • Extracting Event Log Information:
    Get-EventLog -LogName Application | Select-Object EntryType, Source, Message -First 10
    

    This command retrieves the first 10 entries from the Application event log and selects specific properties.

Conclusion

The Select-Object cmdlet is a powerful tool in PowerShell for manipulating and selecting data. Whether you’re filtering process information, managing files, or working with event logs, understanding how to use Select-Object effectively can greatly enhance your scripting capabilities. By mastering its parameters and options, you can streamline your workflows and handle data more efficiently.

PowerShell’s versatility, combined with cmdlets like Select-Object, makes it an indispensable tool for system administrators and developers alike. Experiment with the examples provided and incorporate Select-Object into your daily tasks to see its full potential in action.


Discover more from Parveen Singh

Subscribe to get the latest posts sent to your email.

Recommended Readings

Discover more from Parveen Singh

Subscribe now to keep reading and get access to the full archive.

Continue reading