Windows PowerShell Select-Object

Examples of Windows PowerShell Select-Object

PowerShell's Select-Object is ideal for filtering properties before you output them to file, printer or even read them on-screen.  Perhaps the underlying command produces too much information, with Select-Object you can reduce the number of columns passed to the next element, Format-Table or Out-File.

Topics for PowerShell Select-Object

 ♣

Introduction to PowerShell Select-Object

Example: Choose Properties Returned by Get-Process

Let us assume that you want to list the computer's running processes sorted by CPU usage.  The point of piping the output of the 'Sort' phrase into Select-Object is that you get to choose which properties are displayed.  Incidentally, selecting the properties also means that you get to choose the order, in this case, I like the name of the process in the first column.

# PowerShell Select-Object Example
Clear-Host
Get-Process | Sort-Object cpu -descending | `
Select-Object Name, cpu, handles, workingset

Note 1:  You can shorten Select-Object to plain 'Select', this is because PowerShell has an alias of that name.

Note 2:  For this particular job I prefer to use Format-Table because I can achieve the same result and in addition employ -auto to tighten up the columns.

# PowerShell comparing Select-Object with Format-Table
Clear-Host
Get-Process | Sort-Object cpu -descending | `
Format-Table Name, cpu, handles, workingset -auto

Conclusion: What can Select-Object do that Format-Table cannot?  Let investigate the parameters with Get-Help.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Researching Select-Object's Parameters

To see the full list of parameters call for Get-Help

# PowerShell Select-Object Parameters
Clear-Host
Get-Help Select-Object -full

Result: Help reveals that Select-Object has parameters which are not be available if you used Format-Table, for example, -first, -last -expand, -skip and -unique.

# PowerShell Select-Object Parameters
Clear-Host
Get-Process | Sort-Object cpu -descending | `
Select-Object name, cpu, handles, workingset -first 10

Comparing the Speed of Select-Object with Format-Table

Let us try an experiment; we will measure the time it takes to complete the same task but with one difference, filtering with Select-Object or FT (Format-Table).

Experiment A: Format-Table Speed Test

# PowerShell comparing the speed of Format-Table with Select
Clear-Host
Measure-Command {
Get-Process | Sort-Object WorkingSet -descending | `
Format-Table Name, cpu, handles, workingset -auto
}

Result: 75 TotalMilliseconds (fastest of 3 to compensate for caching)

Experiment B: Select-Object Speed Test

# PowerShell Select-Object Speed Test
Clear-Host
Measure-Command{
Get-Process | Sort-Object WorkingSet -descending | `
Select-Object Name, cpu, handles, workingset
}

Result: 28 TotalMilliseconds (fastest of 3 to compensate for caching)

Conclusion: Select-Object is much faster than Format-Table, consequently I only use Format-Table when I need the -Auto feature.

See more on PowerShell's Measure-Object cmdlet ยป

 

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer's Toolset v10

This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it's like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer's Toolset now!

Download your fully functional trial copy of the Engineer's Toolset v10

Other Uses of Select in PowerShell Cmdlet

There are two other ways that PowerShell uses 'Select'.

Perhaps the most common way is to employ Select in a WMI Query Language (WQL) statement.  Such Get-WmiObject examples which use '-query' to introduce a classic 'Select *' phrase.

The second context for 'Select' in PowerShell is Select-String.  This cmdlet not only opens a file, but also checks for a word, a phrase, or in fact any pattern match. 

More PowerShell Object Cmdlets

You can use Get-Command to research more of PowerShell's 'Object' family.  All you need to do is append the -Noun parameter.  Incidentally, you could also try -Verb Select.

Clear-Host
Get-Command -Noun Object

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax

PowerShell Home  • Syntax  • ISE (GUI)  • Pipeline  • Format-Table  • Select-Object

Compare-Object  • Compare-Object (Registry)  • Group-Object  • Sort-Object  • Foreach

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: