PowerShell Magic with Tee-Object: Have Your Output and Use It Too

Have you ever run a PowerShell command and thought,

Gosh, I wish I could save this output and keep using it in the same pipeline!

That’s where Tee-Object comes in — like a benevolent wizard that hands you a copy of the scroll and sends it off to the next stage of your quest.

Let’s explore how it works, with real-world examples and just a smidge of whimsy.

This post is a companion for the video embedded below. Scroll down to see the code from the video.

 

The Out-File Conundrum

Say you want to write the output of a command to a file:

Get-Process msedge | Out-File -FilePath .\processes.txt
Get-Content -Path .\processes.txt

This dutifully saves the list of running msedge processes to processes.txt. Wonderful! But… the output disappears from your console like a shy raccoon.

You want both—visibility and a file. Enter: Tee-Object.


Tee-Object to the Rescue

Get-Process msedge | Tee-Object -FilePath .\processes.txt
Get-Content -Path .\processes.txt

Now you’ve saved the output and it still shows in the terminal. No sleight of hand, just a better way to log while staying in the flow.


Capturing Output to a Variable and Filtering It

Old-school way:

$Processes = Get-Process msedge
$Processes | Out-File -FilePath .\processes.txt
$Processes | Format-Table

This works, but it takes three steps. We can do better.

The streamlined Tee-Object version:

$secHf = Get-HotFix | Tee-Object -Variable AllHf | Where-Object Description -eq 'Security Update'
$AllHf
$secHf
  • $AllHf contains everything from Get-HotFix
  • $secHf contains just the security updates
  • And you did it in one glorious pipeline

Logging in a Loop? No Problem

Here’s how you’d normally log each item in a loop:

1..3 | ForEach-Object {
    "Item $_"
    Add-Content -Path .\log.txt -Value "Item $_"
}

Effective. Also mildly clunky.

Now with Tee-Object:

1..3 | ForEach-Object {
    "Item $_" | Tee-Object -FilePath .\log.txt -Append
}

It’s cleaner, prettier, and 34% more likely to impress your coworkers (citation needed).


Fun Fact: Don’t Verb Like That

Here’s a little PowerShell trivia for your next IT team stand-up:

Get-Verb | Where Verb -match '^T'

You’ll see that Tee is a reserved verb in PowerShell. That means you should not write your own function called Tee-YourCoffee. It might work… until it doesn’t. And when it doesn’t, PowerShell will laugh at you in silence.


Summary

Tee-Object is your pipeline sidekick:

  • -FilePath saves to a file
  • -Variable saves to a variable
  • Keeps output moving down the pipeline
  • Perfect for debugging, logging, and keeping things clean
  • Makes you look like a scripting wizard with half the effort

So next time you need to log, store, and continue processing—all at once—just remember:

“When in doubt, Tee it out.”*

*Not actually trademarked. But it should be.