The Windows equivalent of the Unix 'tee' command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# The Windows equivalent of the Unix 'tee' command

The Unix tee command reads standard input and writes the output of a program to standard output and copies it to the specified file.

Use the PowerShell Tee-Object command as the Windows equivalent of the Unix tee command. The Tee-Object command is used to save the output of a command in a file or variable and send it down the pipeline.

Open PowerShell and run the following command.

PowerShell
dir | tee my_file.txt

using powershell tee object tee command

Notice that we can use the tee command in PowerShell.

We are actually using the Tee-Object PowerShell command which is also accessible by its alias - tee.
PowerShell
# 👇️ the same as above dir | Tee-Object my_file.txt

The command in the example uses the dir command to display a list of a directory's files and subdirectories and saves the output of the command in a file.

You can also specify a path to the file where the command should save the object.

PowerShell
dir | tee -FilePath "C:\Users\Public\bobbyhadz\my_file.txt"

specify file path with tee command

Make sure to update the path to the file with your specific path.

You can use this approach to save the output of any command in a file or a variable.

PowerShell
Get-Process | tee -FilePath "C:\Users\Public\bobbyhadz\my_file.txt"

Note that you can replace tee with Tee-Object in any of the commands above.

You can use the Get-Help Tee-Object command to display the help page of Tee-Object with information about the syntax, the command's aliases and some helpful remarks.

PowerShell
Get-Help Tee-Object

You can also check out the command's syntax and some examples in the official docs.

Alternatively, you can use the tee command in Git Bash on Windows.

If you already have git installed, you can search for Git Bash and use the tee command, otherwise, you have to install git first.

# Using the tee command on Windows in Git Bash

To download git and be able to use Git Bash:

  1. Open the git downloads page and download the installer for Windows.
  2. Start the installer.
  3. You will be prompted to select a destination location. You can leave the default option and click Next.

git select destination location

  1. You will be prompted to select components on the next screen. Leave the default options and click Next.

git select components

  1. Click Next on the screen that prompts you to "Select Start Menu Folder".

  2. On the next screen, you can choose the default editor for Git, e.g. Notepad, Notepad++ or any other editor you prefer.

choose git default editor

  1. On the "Adjust the name of the initial branch in new repositories screen", click Next.

adjust name of initial branch

  1. On the "Adjust your PATH environment" screen, make sure you have the default option of "Git from the command line and also from 3rd-party software" option selected and click "Next".

adjust your path environment

  1. For all the remaining screens, leave the default option selected and click Next.
  2. Lastly, click on the Install button to install git.

Once you have git installed, click on the Search field, type "Git Bash" and start the application.

search for git bash

Now you can use the tee command directly in Git Bash.

GitBash
dir | tee my_file.txt

using tee command on windows in git bash

If you need to open Git Bash in a specific folder:

  1. Open the folder in a new window.
  2. Right-click in Explorer.

open git bash in folder

  1. Click "Git Bash Here".

Finally, issue the tee command.

GitBash
dir | tee my_file.txt

You can also specify a path to the file you want to tail.

GitBash
dir | tee "C:/Users/Public/bobbyhadz/my_file.txt"

using tee command on windows with path

Make sure to replace the path from the example with your specific path.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.