Select-Object

Learn how to process data with Select-Object.

Download this Lecture

In this video, we look at how to use Select-Object to process data on the PowerShell pipeline. You can find the sample code from this video below.

#region Property
Get-Process | Select-Object -Property Name 
Get-Process | Select-Object -Property Name, ID 
Get-Process | Select-Object -Property Name | Get-Member

Get-LocalUser | Select-Object -Property Name, { $_.SID.Value }
Get-LocalUser | Select-Object -Property Name, @{ label = "SID"; expression = { $_.SID.Value } }
Get-LocalUser | Select-Object -Property Name, @{ l = "SID"; e = { $_.SID.Value } }

$Property = @{ l = "SID"; e = { $_.SID.Value } }
Get-LocalUser | Select-Object -Property Name, $Property
#endregion

#region ExpandProperty
Get-Process | Select-Object -ExpandProperty Name 
Get-Process | Select-Object -ExpandProperty Name | Get-Member

Get-Process code | Select-Object -Property ProcessName -ExpandProperty Modules -First 1 | Format-List 
#endregion

#region ExcludeProperty
Get-LocalUser
Get-LocalUser | Select-Object -ExcludeProperty Description 
Get-LocalUser | Select-Object -ExcludeProperty Description | Get-Member
#endregion ExcludeProperty

#region Unique
@(1, 1, 1, 2, 3, 4) | Select-Object -Unique 
@(1, 1, 1, 2, 3, 4) | Select-Object -Unique -First 3
#endregion 

#region Paging and Waiting
function Get-Object {
    [CmdletBinding()]
    param([int]$Count = 5)

    End {
        1..$Count | ForEach-Object {
            Write-Verbose "Processing $_"
            @{ Prop = $_ }
            Start-Sleep -Seconds 1
        }
    }
}

Get-Object | Select-Object -First 2
Get-Object | Select-Object -First 2 -Skip 1
Get-Object | Select-Object -Last 1
Get-Object | Select-Object -SkipLast 1
Get-Object | Select-Object -Index 2, 3
Get-Object | Select-Object -SkipIndex 2, 3

Get-Object -Verbose | Select-Object -First 2
Get-Object -Verbose | Select-Object -First 2 -Skip 1

Get-Object -Verbose | Select-Object -First 1 #-Wait
#endregion