Tip: This cmdlet Tee-Object is a “2 in 1”, it performs 2 operations at the same time:
1 – Output to a file or variable
2 – Output to the console
Without Tee-Object (Action 1 and Action 2 are “separate”)
1 2 |
# Action 1 > Output to a file Get-Service | Out-File -FilePath 'C:\Demo\services.txt' |
1 2 |
# Action 2 > Output to the console (by reading the file) Get-Content -Path 'C:\Demo\services.txt' |
With Tee-Object (Action 1 and Action 2 are “simultaneous”)
Case 1 : Output to a file (+ console)
1 2 3 |
#requires -Version 3 # Output to console + save to a file Get-Service | Tee-Object -FilePath 'C:\Demo\services.txt' |
Case 2 : Output to a variable (+ console)
1 2 3 |
#requires -Version 3 # Output to console + save to a variable Get-Service | Tee-Object -Variable services |