---
title: PowerShell Tee-Object: Redirect Output and Keep It in the Pipeline
description: Learn how to use the PowerShell Tee-Object cmdlet to redirect output from a command while still keeping it in the pipeline.
canonical: https://adamtheautomator.com/tee-object/
---

# PowerShell Tee-Object: Redirect Output and Keep It in the Pipeline

> Learn how to use the PowerShell Tee-Object cmdlet to redirect output from a command while still keeping it in the pipeline.

Source: https://adamtheautomator.com/tee-object/

---

- PowerShell Tee-Object: Redirect Output and Keep It in the Pipeline Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# PowerShell Tee-Object: Redirect Output and Keep It in the Pipeline

        Published:15 June 2019 - 2 min. read

- PowerShell

          [](https://adamtheautomator.com/author/adam-bertram/)

            [Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)

            Read [more tutorials](https://adamtheautomator.com/author/adam-bertram/) by Adam Bertram!

-

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

Table of Contents

- Using Tee-Object with a File
- Using Tee-Object with a Variable
- Wrap up

                XFacebookLinkedIn

Tee-Object is one of those PowerShell cmdlets that's always been there but rarely used. I can count on two hands in my nearly 10 years of PowerShell development work that I've needed or even thought to use this cmdlet but when I do, it works great!

## Using Tee-Object with a File

What does Tee-Object do anyway? Tee-Object basically kills two birds with one stone. This cmdlet redirects output from a [PowerShell](https://adamtheautomator.com/tag/powershell/)command and either saves it to a file or to a variable while also returning the output to the pipeline. It allows a scripter to save the output and send the output across the pipeline all in one shot.

Let's say a developer wants to send some text to a file and then perform kind of task on that same text afterward. Perhaps we're pulling a list of Windows services from a computer and saving that output to a text file to create a snapshot of the before state before we attempt to start the services. This task can be done in two separate steps or with a single one with Tee-Object.

Without Tee-Object, this task may look something like this:

PS> $servicesToStart = 'wuauserv','WebClient'
PS> Get-Service -Name $servicesToStart | Out-File -FilePath 'C:\ServicesBefore.txt'
PS> Get-Service -Name $servicesToStart | Start-Service

This method works but there's a better way with Tee-Object . The Tee-Object cmdlet has a FilePath parameter we can use to save the output to a file. Here's what this same task would look like with Tee-Object. Notice how we're using the pipeline to transfer the objects from one command to the next.

PS> $servicesToStart = 'wuauserv','WebClient'
PS> Get-Service -Name $servicesToStart | Tee-Object -FilePath 'C:\ServicesBefore.txt' | Start-Service

This method always replaces the text in a file. If we would have rather appended the text in the file, we could have used the Append switch parameter.

## Using Tee-Object with a Variable

The Tee-Object PowerShell cmdlet also has a variable parameter that, instead of sending output to a file, it sends the output to a variable. Instead of first assigning the output of Get-Service to the variable $serviceStatusBefore, we can simply use the Variable parameter on Tee-Object to create the variable and send the output there. Then, since Tee-Object always returns the input it receives back to the pipeline, we can use that as input to Start-Service as we did in the above example.

PS> $servicesToStart = 'wuauserv','WebClient'
PS> Get-Service -Name $servicesToStart | Tee-Object -Variable 'serviceStatusBefore' | Start-Service
PS> $serviceStatusBefore
Status Name DisplayName
------ ---- ------------
Running WebClient WebClient
Running wuauserv Windows Update

## Wrap up

Tee-Object is one of those PowerShell commands that aren't necessarily required in your PowerShell code but make it cleaner and less dense. Using Tee-Object allows a developer to cut down on the number of lines of code yet performs exactly the same task. Granted, eliminating code does sometimes make the code a little less readable but that's up to the developer to decide on that tradeoff!

Whenever you need to either save command output to a file or to a variable, take a look at the Tee-Object cmdlet.

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
