PowerShell Tee-Object Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Measure-Object cmdlet in PowerShell.
https://cloudaffaire.com/powershell-measure-object-cmdlet/
In this blog post, we will discuss Tee-Object cmdlet in PowerShell. You can use Tee-Object cmdlet to save command output to a file or to a variable. Tee-Object also displays the output in the prompt apart from saving to a variable or file. If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt.
Tee-Object Cmdlet Syntax:
1 2 3 4 5 6 7 |
## Tee-Object ## [-InputObject ## [-FilePath] ## [-Append] ## -LiteralPath ## -Variable ## [ |
Tee-Object Cmdlet Argument List:
- –Append: Indicates that the cmdlet appends the output to the specified file. Without this parameter, the new content replaces any existing content in the file without warning.
- –FilePath: Specifies a file that this cmdlet saves the object to Wildcard characters are permitted, but must resolve to a single file.
- –InputObject: Specifies the object to be saved and displayed. Enter a variable that contains the objects or type a command or expression that gets the objects. You can also pipe an object to Tee-Object.
- –LiteralPath: Specifies a file that this cmdlet saves the object to. Unlike FilePath, the value of the LiteralPath parameter is used exactly as it is typed.
- –Variable: Specifies a variable that the cmdlet saves the object to. Enter a variable name without the preceding dollar sign ($).
PowerShell Tee-Object Cmdlet:
Save Output Of A Cmdlet To A Variable And Also Display The Output To Command Line In PowerShell:
1 2 3 4 5 |
## save output of a cmdlet to a variable and also display to the command line Get-Process | Select-Object -First 5 | Tee-Object -Variable myvar $myvar ## prints the output |
Save Output Of A Cmdlet To A File And Also Display The Output To Command Line In PowerShell:
1 2 3 4 5 |
## save output of a cmdlet to a file and also display to the command line Get-Process | Select-Object -First 5 | Tee-Object -FilePath .\myfile.txt Get-Content .\myfile.txt ## prints the output |
Save Output Of A Cmdlet To A File Appending To The Existing Content And Also Display The Output To Command Line In PowerShell:
1 2 3 4 5 |
## save output of a cmdlet to a file appending with existing content Get-Process | Select-Object -Last 5 | Tee-Object -FilePath .\myfile.txt -Append Get-Content .\myfile.txt ## prints the output |
Save Output Of A Cmdlet To A File And A variable Simultaneously And Also Display The Output To Command Line In PowerShell:
1 2 3 4 5 6 |
## save output of a cmdlet to a file and a variable simultaniously and also display to the command prompt Get-Process | Select-Object -First 5 | Tee-Object -FilePath .\myfile.txt -OutVariable myvar1 $myvar1 Get-Content .\myfile.txt ## prints the output |
Hope you have enjoyed this article. In the next blog post, we will discuss Compare-Object cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation
how do I use powershell Tee with Tar like in Un*x ?
tar cf – * | tee tar xf -C \backup1 | tar xf -C \backup2
The powershell tee seems to be crippled like the write-output , ie with some wierd “we know better” internal issues.