In this blog post, we will discuss about Tee-Object cmdlet in PowerShell. Previously I am not got a chance to use this cmdlet in any of my automation and currently using the same in my current work. Due to this, I thought to write a post on this topic which is useful while scripting for others.
You can use the Tee-Object cmdlet to save command output to a file or to a variable. In simple, the Tee-Object cmdlet Saves command output in a file or variable and also sends it down the pipeline (If Tee-Object is the last command in the pipeline, the command output is displayed in the console).
Why do we need to use Tee-Object?
Before I tell you what the Tee-Object cmdlet does, we have to take a small time to talk about the normal way of doing things. Typically, when you use a Get cmdlet (Get-VM, Get-Service, Get-Process, etc.) you will end up doing one of two things.
- The first option is, we will send the output down the pipeline, and then either display the output on the screen or write it to a file.
- The other option is to write the cmdletโs output to a variable.
But, in case if you want to have both then? for this question, we need to have Tee-Object.
Example 1: Normal Scripting without Tee-Object
The first line of code captures the Get-Process cmdletโs output (get the notepad process) and assigns it to a variable named $processDetails. The second line of code displays the contents of the variable to the screen (as shown in the snapshot).
Hence, these two lines of code’s objective are to display the output on screen and also write it to a variable.
$processDetails = Get-Process notepad
$processDetails
Example 2: Using Tee-Object
Now in this example, letโs take a look at how the same thing might be done using the Tee-Object cmdlet.
The Tee-Objectcmdlet allows output to be written to the screen and simultaneously written to either a file or a variable. In this below example, the output is written in the variable called processDetails
Get-Process notepad | Tee-Object -Variable processDetails
Example 3: Using Tee-Object cmdlet in the middle of the command pipeline
You can also use the Tee-Object cmdlet to be inserted in the middle of the command pipeline as shown below.
If you see the output (from the below snapshot) we are also able to see that the variableโs contents ($notepadProcessDetail) are not an exact match for what is being displayed onscreen, because we declared the variable before using the Select-Object cmdlet.
Get-Process notepad | Tee-Object -Variable notepadProcessDetails | Select-Object ProcessName ,CPU
$notepadProcessDetail
Example 4: Using Tee-Object cmdlet in the Last of the command pipeline
In case, you decided to have the Tee-Object at end of the command pipeline then the variableโs contents will be same as shown in the below snapshot.
Get-Process notepad | Select-Object ProcessName ,CPU | Tee-Object -Variable notepadProcessDetails
$notepadProcessDetail
Example 5: Use Tee-Object for writing output to a file
You can also use the Tee-Object cmdlet to write a commandโs output to a file, we want to dump the commandโs output to a file instead of writing it to a variable, for this we can do like below.
Get-Process notepad | Tee-Object -FilePath โC:\dotnet-helpers\Details.txtโ
You can use the Tee-Object command to declare a variable from the middle of the pipeline (as shown in example 3) or we can also write a file from the middle of the pipeline. In the above example, we wrote in the file rather than using the variable.
Leave A Comment