Tee Object - PowerShell


Overview

Tee-Object is a PowerShell command that creates a duplicate of the input objects and sends them to the pipeline and a specified file or command. It is commonly used for logging, debugging, and monitoring purposes by allowing you to capture and persist data while still continuing to process it in the pipeline.

Syntax

Tee-Object [-FilePath] <string> [-Append] [-ErrorAction <string>] [-Force] [-PassThru] [-Stream <string>]

Options/Flags

| Option | Description | Default |
|—|—|—|
| -FilePath | Specifies the file path where the duplicated objects should be saved. | N/A |
| -Append | Specifies whether to append the objects to an existing file (if it exists) or overwrite it. | False |
| -ErrorAction | Specifies the action to take when an error occurs. Valid values are: Stop, Continue, SilentlyContinue, Inquire, Ignore. | Continue |
| -Force | Specifies whether to overwrite an existing file without prompting for confirmation. | False |
| -PassThru | Specifies whether to pass the duplicated objects through the pipeline in addition to sending them to the file or command. | False |
| -Stream | Specifies the stream to write the objects to. Valid values are: Output, Error, Warning, Verbose, Debug. | Output |

Examples

Example 1: Logging Objects to a File

Get-Process | Tee-Object -FilePath c:\temp\processes.log

Example 2: Viewing and Logging Objects in Parallel

Get-Service | Tee-Object -PassThru | Format-Table -AutoSize

Example 3: Capturing Error Messages

Get-ChildItem -Path c:\nonexistentfolder -ErrorAction SilentlyContinue | Tee-Object -Stream Error

Common Issues

  • File Permissions: Ensure you have the necessary file permissions to overwrite or append to the specified file.
  • Object Size: Note that Tee-Object duplicates the entire input objects, so be cautious when dealing with large objects to avoid performance issues.

Integration

Tee-Object can be combined with other commands to perform more advanced tasks:

  • Logging and Monitoring: Pipe the output of commands to Tee-Object to capture and save important data for analysis or troubleshooting.
  • Debugging: Use Tee-Object to create a snapshot of objects at specific points in a script or pipeline for later inspection.
  • Data Manipulation: Modify the duplicated objects before passing them to the file or command using additional PowerShell commands.
  • Write-Output
  • Out-File
  • Format-Table