Windows PowerShell Tee-Object

PowerShell Tee-ObjectWindows PowerShell Tee-Object Cmdlet

The point of Tee-Object is that it can do two thing at once, for example, it could save the output to a file, while redirecting the output to the console.

Think of 'T' as in T-junction, and you will appreciate what this cmdlet can do when you need duplicate outputs.

'Tee' in Windows PowerShell


Example 1: Eventlog and Tee-Object

The scenario: We want to display entries in the System eventlog; furthermore, we want to save these entries into a named file. 

# Tee-Object example, trace the branching to $File and Format-Table
$File = "D:\PShell\EventStuff.txt"
Get-EventLog System -Newest 100 -EntryType Error |
Tee-Object -FilePath $File |
Format-Table TimeGenerated, EventId, Source -AutoSize

Warning: To save disappointment, please modify the value of $File before executing this example on your machine.

Note 1: To see the file contents you could append:
Invoke-Item $File

Note 2: When Tee-Object is the last command in the pipeline, the output is sent to the console.

Challenge: Modify the script to delete all entries with a TimeWritten before 30 days, while keeping a copy of the names of the entries deleted.

Investigate Parameters of Tee-Object

As usual Get-Help reveals useful parameters such as -Append, which would be handy if we wanted the file to keep a record of similar errors over a long period of time.

# Investigate parameters and examples
Get-Help Tee-Object -Full

Note 3: Parameters such as -Variable would be useful in creating other scripts.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2: Tee-Object Saves Data Into Two Files

One of the commonest jobs for Tee-Object is where you want to save data to files.  One file holds the latest results, whereas the other file keeps a running total.

Scenario: You want to save the current eventlog error messages to a file called NewOnly.txt.  However, you also want to keep a running total in a file called FullRecord.txt.

# Tee-Object branching with two different files
Function Get-SystemLogRecord {
Param (
[String]$NewOnly = "D:\PShell\NewOnly.txt",
[String]$Continuous = "D:\PShell\FullRecord.txt"
Begin {
"Start ….`n"
Get-EventLog System -Newest 100 -EntryType Error
             } # Conclusion of function's Process engine
End {
"`n Latest report " + (Get-Date).ToString()
} # Function code complete

# Now, here is where Tee-Object uses the Function
Get-SystemLogRecord |
Tee-Object -FilePath $NewOnly |
Out-File $Continuous -append
Invoke-Item $Continuous

If you are not familiar with functions, then here is the bare-bones version of the above script:

$NewOnly = "D:\PShell\NewOnly.txt"
$Continuous = "D:\PShell\Continuous.txt"
Get-EventLog System -Newest 25 -EntryType Error |
Tee-Object -FilePath $NewOnly |
Out-File $Continuous -append
Invoke-Item $Continuous

Note 4: One output goes to -FilePath, and the other branches to Out-File.

Guy Recommends:  SolarWinds’ Log & Event Management ToolSolarwinds Log and Event Management Tool

LEM will alert you to problems such as when a key application on a particular server is unavailable.  It can also detect when services have stopped, or if there is a network latency problem.  Perhaps this log and event management tool’s most interesting ability is to take corrective action, for example by restarting services, or isolating the source of a maleware attack.

Yet perhaps the killer reason why people use LEM is for its compliance capability, with a little help from you, it will ensure that your organization complies with industry standards such as CISP or FERPA.  LEM is a really smart application that can make correlations between data in different logs, then use its built-in logic to take corrective action, to restart services, or thwart potential security breaches – give LEM a whirl.

Download your FREE trial of SolarWinds Log & Event Management tool.

Example 3: The Rhythm of Tee-Object Cmdlet

With Tee-Object you can run a command-line application in PowerShell and have the output both redirected to a file, and displayed on the console.  For example:

$File ="C:\ProgramData\"
Get-ChildItem $File | Tee-Object -FilePath "C:\Temp\TeeProgDat.txt" |
Format-Table Name

Note 5: Observe the rhythm of the command, and understand the position of PowerShell's (|) pipes.

  1. Input |
  2. Tee-Object (no pipe yet) 1st output follows directly
  3. | (pipe after first output)
  4. 2nd output.

Tee-Object Alias

# Investigate alias
Get-Alias -Definition Tee-Object

CommandType  Name ModuleName
—————-  ——   ———-
Alias              tee ->  Tee-Object

Incidentally, Tee-Object's alias reminds me of the Tee command in UNIX, which is also used to both store and view the output from another command.

Example 4: Tee-Object Sequence Problem

I find that if you change the sequence after Tee, and try to place the console output BEFORE the file output, then the script fails.

Example A: Problem

# Example A: Failure
$File ="C:\ProgramData\"
Get-ChildItem $File | Tee Format-Table Name |
-FilePath "C:\Temp\TeeProgDat.txt" -Append

-FilePath : The term '-FilePath' is not recognized as the name of a cmdlet, function, script file, or operable program.

Note 6: There is nothing wrong with using the alias 'Tee' for Tee-Object; as you can see in Example C below.  It's the sequence that's the problem.

Example B: Problem. This sequence does not work either:

# Example B: Another failure
$File ="C:\ProgramData\"
Get-ChildItem $File | Tee | Format-Table Name |
-FilePath "C:\Temp\TeeProgDat.txt" -Append

Example C: Solution. Place the instruction for outputting to the console last.

# Example C: Success!
$File ="C:\ProgramData\"
Get-ChildItem $File | Tee -FilePath "C:\Temp\TeeProgDat.txt" -Append |
Format-Table Name

Note 7: You could add:
Invoke-Item "C:\Temp\TeeProgDat.txt"

What's new in PowerShell 4.0 »

Summary of PowerShell Tee-Object

The purpose of Tee-Object is to act as a 'T' junction in PowerShell's pipe sequence.  This means that your script can have two outputs; in the classical example it takes an input and sends one output to a file, and another to the console.

If you like this page then please share it with your friends


See more Microsoft PowerShell output tutorials:

PShell Home   • Out-File   • Out-GridView   • ConvertTo-Csv   • ConvertTo-Html   • ConvertFrom-Csv

Tee-Object   • Import-CSV   • Format-Table   • PowerShell Here-String  • ConvertFrom-JSON

Export-CliXml   • Format-List   • Read-Host    • PowerShell Get-History   • -f format   • Pipe to file

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.