개요
Tee-Object(별칭 tee)는 파이프라인 결과를 파일이나 변수에 저장하면서, 동시에 그 결과를 그대로 다음 명령으로 계속 흘려보내는 cmdlet이다. 이름과 동작 모두 Unix tee(수도관을 T자로 분기한다는 뜻)를 그대로 계승했다. Out-File이나 > 리다이렉션이 파이프라인을 그 자리에서 끝내고 파일에만 저장하는 것과 달리, Tee-Object는 저장과 동시에 파이프라인을 계속 이어간다는 점이 다르다.
정신 모델은 “관측하고 싶은 중간 지점에 분기점을 꽂아 두는 것"이다. 디버깅 중인 긴 파이프라인의 중간 결과를 파일로 남기거나 변수에 담아 두고 싶을 때, 파이프라인 자체를 두 번 실행할 필요 없이 Tee-Object 한 줄만 끼워 넣으면 된다.
사용법
| |
매개변수
| 매개변수 | 설명 |
|---|---|
-FilePath | 결과를 저장할 파일 경로(와일드카드는 단일 파일로 귀결되어야 함) |
-LiteralPath | 와일드카드를 문자 그대로 취급하는 경로(파일명에 *, [] 등이 실제로 포함된 경우) |
-Variable | 결과를 저장할 변수 이름($ 기호 없이 지정) |
-Append | 기존 파일 내용에 이어 붙인다(기본은 덮어쓰기, 경고 없음) |
-Encoding | 저장할 파일의 인코딩(PowerShell 6+ 기본값은 BOM 없는 UTF-8) |
예시
| |
주의사항·함정
-Append 없이는 경고 없이 기존 파일이 지워진다: -FilePath만 지정하면 기존 파일 내용을 확인 없이 덮어쓴다. 로그처럼 누적해야 하는 파일이라면 반드시 -Append를 함께 써야 한다.
변수로 저장한 결과에는 파이프라인의 전체 출력 정보가 포함된다: -Variable proc으로 저장한 뒤 이어지는 Select-Object에서 속성을 제한해도, $proc 변수 자체에는 원래의 전체 속성을 가진 객체가 그대로 담긴다 — Tee-Object는 자신의 뒤에 오는 명령이 무엇을 하든 관여하지 않고, 오직 자신을 거쳐 가는 시점의 객체를 저장할 뿐이다.
-WhatIf를 직접 지원하지는 않는다: Tee-Object 자체는 -WhatIf 매개변수가 없지만, 내부적으로 Set-Variable·Out-File을 사용하므로 [CmdletBinding(SupportsShouldProcess)]로 작성한 함수·스크립트 안에서 호출되면 그 상태(-WhatIf)를 이어받아 실제로 파일에 쓰지 않고 시뮬레이션만 한다.
이식성: Unix tee와 이름·역할이 같지만, PowerShell 버전은 파일 저장 외에 세션 변수 저장(-Variable)까지 지원한다는 점이 다르다. CMD에는 대응하는 명령이 없다.
![Featured image of post [PowerShell] 18. Tee-Object — 파이프라인 분기](/post/powershell/tee-object-command-pipeline-branch-powershell/wordcloud_hu_e0ad28b7841fc2be.webp)
![[PowerShell] 16. Group-Object — 그룹화](/post/powershell/group-object-command-grouping-powershell/wordcloud_hu_2e19558643be3d4e.webp)
![[PowerShell] 17. Measure-Object — 집계·통계](/post/powershell/measure-object-command-aggregate-statistics-powershell/wordcloud_hu_f090ca1c28bf8f73.webp)
![[PowerShell] 18. Tee-Object — 파이프라인 분기](/post/powershell/tee-object-command-pipeline-branch-powershell/wordcloud_hu_26906348220bc5ce.webp)
![[PowerShell] 19. Compare-Object — 객체 비교(diff)](/post/powershell/compare-object-command-diff-powershell/wordcloud_hu_85c73f8cde4535c3.webp)
![[PowerShell] 20. 비교 연산자 — -eq/-like/-contains/-in](/post/powershell/comparison-operators-eq-like-contains-in-powershell/wordcloud_hu_e39e3d06b7800d57.webp)
![[PowerShell] 50. ConvertTo-Html과 Out-File](/post/powershell/convertto-html-out-file-powershell/wordcloud_hu_d068d41aa5ff8785.webp)
![[PowerShell] 24. 자동 변수 총정리 — $_, $null, $Error, $LASTEXITCODE](/post/powershell/automatic-variables-powershell-underscore-null/wordcloud_hu_fed154e062984d25.webp)
![[PowerShell] 25. 공통 매개변수 — -Verbose/-Debug/-ErrorAction/-OutVariable](/post/powershell/common-parameters-verbose-debug-erroraction-powershell/wordcloud_hu_af2f3755ebbce019.webp)
![[PowerShell] 29. Write-Host vs Write-Output — 출력 스트림 오개념 정리](/post/powershell/write-host-vs-write-output-streams-powershell/wordcloud_hu_cd68b845a7c59766.webp)
![[PowerShell] 34. Copy-Item — 복사](/post/powershell/copy-item-command-copy-file-powershell/wordcloud_hu_ebeda4bcc9a1c9ff.webp)