PowerShell Select-Object cmdlet은 파일프라인을 통해 개체의 속성을 선택해 사용자 지정 개체를 만들 수 있다.
Select-Object
[[-Property] <Object[]>]
[-InputObject <psobject>]
[-ExcludeProperty <string[]>]
[-ExpandProperty <string>]
[-Unique]
[-Last <int>]
[-First <int>]
[-Skip <int>]
[-Wait]
[<CommonParameters>]
Get-Service cmdlet을 실행하여 시스템의 모든 서비스 목록을 가져오고 출력을 파이프라인으로 전달하고 Select-Object cmdlet으로 하여 각 서비스 개체의 이름 속성만 선택하고 표시한다.
Get-Service | Select-Object -Property Name
## Output
## Name
## ----
## AarSvc_f241b
## AJRouter
## ALG
## AnySign4PC Launcher
## aoservice
## AppHostSvc
## .
## .
## .
다음은 -first 스위치를 사용해 반환할 개체의 수를 제한한다.
Get-Service | Select-Object -Property Name -First 5
## Output
## Name
## ----
## AarSvc_f241b
## AJRouter
## ALG
## AnySign4PC Launcher
## aoservice
ExpandProperty는 속성을 지정하고 해당 속성의 확장 및 값을 출력한다.
Get-Service cmdlet을 실행하면 시스템의 모든 목록이 배열이고 ExpandProperty를 사용해 Name을 출력하고 표시한다.
Get-Service | Select-Object -ExpandProperty Name -First 5
## Output
## AarSvc_f241b
## AJRouter
## ALG
## AnySign4PC Launcher
## aoservice
Property와 ExpandProperty의 차이는 출력값에 해당 속성의 헤더가 포함되었는지의 여부이다.
Get-Process cmdlet을 실행하면 모든 프로세스 목록이 출력되고 그중 반환할 개체의 수를 1개로 제한하고 Propery와 ExpandProperty를 사용해 출력한 예이다.
Get-Process | Select-Object -First 1 -Property Modules
## Output
## Modules
## -------
## {System.Diagnostics.ProcessModule (Aac3572DramHal_x86.exe), System.Diagnostics.ProcessModule (ntdll.dll), System.Diagnostics.ProcessModule (wow64.dll), System.Diagnostics.ProcessModule (wow64base.dll)...}
Get-Process | Select-Object -First 1 -ExpandProperty Modules
## Output
## Size(K) ModuleName FileName
## ------- ---------- --------
## 2332 Aac3572DramHal_x86.exe C:\Program Files\ASUS\ASUS_Aac_DRAM\Aac3572DramHal_x86.exe
## 2128 ntdll.dll C:\WINDOWS\SYSTEM32\ntdll.dll
## 348 wow64.dll C:\WINDOWS\System32\wow64.dll
## 36 wow64base.dll C:\WINDOWS\System32\wow64base.dll
## 556 wow64win.dll C:\WINDOWS\System32\wow64win.dll
## 88 wow64con.dll C:\WINDOWS\System32\wow64con.dll
## 40 wow64cpu.dll C:\WINDOWS\System32\wow64cpu.dll
Property출력값에 해당 속성이 개체로 그대로 출력되고 ExpandProperty는 해당 속성의 개체가 확장되어 출력된다.