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:
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:
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:
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:
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:
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.