---
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/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![PowerShell Tee-Object: Redirect Output and Keep It in the Pipeline](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea51d-1.jpg)

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

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)15 June 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Using Tee-Object with a File](#using-tee-object-with-a-file)
*   [Using Tee-Object with a Variable](#using-tee-object-with-a-variable)
*   [Wrap up](#wrap-up)

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:

```powershell
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.

```powershell
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.

```powershell
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.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Ftee-object%2F&text=PowerShell%20Tee-Object%3A%20Redirect%20Output%20and%20Keep%20It%20in%20the%20Pipeline)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Ftee-object%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Ftee-object%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
