The Select-Object command

Select-Object is most frequently used to limit the properties returned by a command. The command is extremely versatile as it enables you to do the following:

  • Limit the properties returned by a command by name:
Get-Process | Select-Object -Property Name, Id 
  • Limit the properties returned from a command using wildcards:
Get-Process | Select-Object -Property Name, *Memory 
  • List everything but a few properties:
Get-Process | Select-Object -Property * -Exclude *Memory* 
  • Get the first few objects:
Get-ChildItem C: -Recurse | Select-Object -First 2
  • Get the last few objects:
Get-ChildItem C: | Select-Object -Last 3 
  • Skip items at the beginning. In this example, this returns the fifth item:
Get-ChildItem C: | Select-Object -Skip 4 -First 1 
  • Skip items at the end. This example returns the third from the end:
Get-ChildItem C: | Select-Object -Skip 2 -Last 1  
  • Expand properties:
Get-ChildItem C: | Select-Object -ExpandProperty FullName
Get-ChildItem $env:SYSTEMROOT*.dll | Select-Object Name, Length -ExpandProperty VersionInfo
  • Select-Object can return -Unique values from arrays of simple values:
1, 1, 1, 3, 5, 2, 2, 4 | Select-Object -Unique
About Get-Unique

Get-Unique may also be used to create a list of unique elements. When using Get-Unique, a list must be sorted first, for example: 1, 1, 1, 3, 5, 2, 2, 4 | Sort-Object | Get-Unique.

Select-Object can also return unique values from arrays of objects, but only if a list of properties is specified or a wildcard is used for the list of properties.

In the following example, we create an object with one property called Number. The value for the property is 1, 2, or 3. There are two objects with a value of 1, two with a value of 2, and two with a value of 3:

PS> (1..3 + 1..3) | ForEach-Object { [PSCustomObject]@{ Number = $_ } }

Number
------
1
2
3
1
2
3

Select-Object can remove the duplicates from the set in this example using the -Unique parameter if a list of properties (or a wildcard for the properties) is set:

PS> (1..3 + 1..3) |
>> ForEach-Object { [PSCustomObject]@{ Number = $_ } } |

>> Select-Object -Property * -Unique

Number
------
1
2
3

When using Get-Member, you may have noticed the PropertySet member type. Select-Object can display the properties within the set. In the following example, Get-Member is used to view property sets, and Select-Object is used to display the first property set (PSConfiguration):

PS> Get-Process -Id $PID | Get-Member -MemberType PropertySet

TypeName: System.Diagnostics.Process

Name MemberType Definition
---- ---------- ----------
PSConfiguration PropertySet PSConfiguration {Name, Id, ...
PSResources PropertySet PSResources {Name, Id, Hand...

PS> Get-Process -Id $PID | Select-Object PSConfiguration

Name Id PriorityClass FileVersion

---- -- ------------- -----------
powershell_ise 5568 Normal 10.0.14393.103 (rs1_release_inmarket.160819-1924)

Select-Object is also able to make new properties. It will build a property if given a name and a means of calculating it (an expression):

Get-Process | Select-Object -Property Name, Id, 
    @{Name='FileOwner'; Expression={ (Get-Acl $_.Path).Owner }} 

In the preceding example, @{} is a hashtable. Hashtables are discussed in Chapter 5, Variables, Arrays, and Hashtables.

Select-Object can change objects

When Select-Object is used with the Property parameter, a new object is created (based on the value Select-Object is working with). For example, the first process may be selected as shown here. The resulting object type is Process: (Get-Process | Select-Object -First 1).GetType().

If Select-Object also requests a list of properties, the object type changes to PSCustomObject: (Get-Process | Select-Object -Property Path, Company -First 1).GetType().

This is important if something else is expected to use the process. For example, Stop-Process will throw an error because the object being passed is not a process, nor is there sufficient information available to determine which process must stop (either the Id or Name properties): Get-Process | Select-Object -Property Path, Company -First 1 | Stop-Process -WhatIf.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.220.114