PowerTip: Send output to file and screen at the same time


Summary: Cloud & Datacenter Management MVP, Thomas Rayner, shows how to easily send data to the output of a script.

Hey, Scripting Guy! Question How can I use Windows PowerShell to send the same data that I’m writing to a file to the standard output of my script?

Hey, Scripting Guy! Answer In Windows PowerShell 5.0, use Tee-Object, for example:

PS C:\WINDOWS\system32> Tee-Object -InputObject 'Some text' -FilePath 'C:\temp\some file.txt'

Some text

PS C:\WINDOWS\system32> Get-Content -Path 'C:\temp\some file.txt'

Some text

Comments (3)

  1. Pipeline Plumber says:

    I get teeing to file and screen. What I miss from my Unix/Linux days is teeing to screen and pipeline, so I can see the current state of things but not have to break the pipeline to do so. I have so far been unable to find the PowerShell equivalent for:

    [pipeline stuff] | tee /dev/con | [more pipeline stuff] | [eventual destination]

    I can Tee-Object to a variable, complete the pipeline then display the variable later, but I don’t seem to be able to tee out to the screen now while the processing continues.

    1. Hello, I read you’re comment and thought to myself that if write-host had a -passthru parameter it’s be exactly what you want.
      Alas it does not, however It then struck me how you could emulate that fairly easily.
      It’s by far not anything amazing, but if I can help a fellow powershell-er out:)

      Function Tee-Host {
      Param (
      [Parameter(Mandatory=$true,
      ValueFromPipeline=$true,
      Position=0)]
      $InputObject
      )

      Process {
      write-host $InputObject

      $InputObject
      }
      }

      PS>@(“foo”,”bar”) | Tee-Host | % { “Pipeline processing of $_” }
      foo
      Pipeline processing of foo
      bar
      Pipeline processing of bar

  2. Tee-Object is always one I forget, its such a handy command.

Skip to main content