Powershell

Tee-Object: The Most Underused Cmdlet in PowerShell

The “Tee-Object” cmdlet in PowerShell is utilized to store the output of the command into a file and also it sends it down to the pipeline. As its name shows “Tee” like the letter “T”, works in two ways. First, it sends the output to a variable or file, and at the same time, it copies the output to the pipeline. In case the “Tee-Object” cmdlet is the last in the pipeline then the output will be displayed at the prompt.

In this write-up, the “Tee-Object” cmdlet will be demonstrated with the aid of several examples.

Tee-Object: The Most Underused Cmdlet in PowerShell

As it is stated that the “Tee-Object” cmdlet sends the output in two ways, one in the output file or variable and the other in the pipeline. Examples demonstrating the stated cmdlet are given below.

Example 1: Use the “Tee-Object” Cmdlet to Send the Output to Both File and Console

This illustration will export the content to a text file and it will also display in the console:

Get-Service | Tee-Object "C:\Docs\New.txt"

According to the above code:

  • First, specify the “Get-Service” cmdlet followed by the pipeline “|” to transfer the output of the previous command to the next.
  • Then, write the “Tee-Object” and assign the target file path:

Let’s verify whether the content was exported to a file or not by executing the “Get-Content” cmdlet and assign the file path:

Get-Content "C:\Docs\New.txt"

Example 2: Get the Specific Process, Display it in the Console and Store it in the Variable Using the “Tee-Object” Cmdlet

This demonstration will display and store the output in the variable:

Get-Process notepad | Tee-Object -Variable info | Select-Object processname,cpu,si

According to the above code:

  • First, write the “Get-Process” cmdlet and assign the value “notepad” to it followed by the pipeline “|”.
  • Then, add the “Tee-Object” cmdlet along with the “-Variable” parameter and assign the value “info” to it.
  • Add another pipeline “|”, add “Select-Object” cmdlet, and assign the stated values separated by a comma:

Let’s verify whether the content was stored in the variable or not by executing the variable:

$info

Example 3: Use the “Tee-Object” Cmdlet to Send the Content into Two Files

This illustration will send the output to the two files simultaneously:

Get-Process | Tee-Object -Filepath C:\Docs\FileA.txt | Out-File C:\Docs\FileB.txt

According to the above code:

  • First, write the “Get-Process” cmdlet followed by the pipeline “|”.
  • Then, write the “Tee-Object” cmdlet.
  • After that, add the “-Filepath” parameter and assign the two target paths separated by a pipeline “|”:

That was all about the PowerShell cmdlet “Tee-Object”.

Conclusion

The cmdlet “Tee-Object” in PowerShell sends or stores the output in two ways. One in the output variable or file and another in the pipeline. If in case the stated cmdlet is at the last of the pipeline then the output will be displayed at the prompt. This post has demonstrated the “Tee-Object” cmdlet with the aid of several examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.