When you don't want to cut off your PowerShell pipeline midstream just to print to console.
This git repo has an accompanying blog post here: https://www.dannymeister.com/2019/03/26/pee-object.html
Pee-Object
prints command output to console and also sends it down the pipeline.
It's like Tee-Object, but for Printing objects. Just as Tee-Object is aliased tee
, Pee-Object
is aliased pee
.
This is often useful on one-liner scripts to debug the results of one step in the pipeline before sending it to the next, without having to break your commands onto multiple lines.
It can also be a poor-developer's progress indicator for long running operations in your pipeline.
Get count of all .txt files on hard drives, and print out each drive letter as you begin to process it.
Get-PSDrive -PSProvider FileSystem | pee | %{Get-ChildItem "$_`:`\" -Filter "*.txt" -Recurse -File -ErrorAction SilentlyContinue} | Measure-Object | select Count
C
D
E
F
G
Count
-----
24007
This didn't seem to deserve a fancy module for importing into your scripts. I would recommend simply dot sourcing the file in your profile so it's handy when the need arises.
In %UserProfile%\My Documents\WindowsPowerShell\profile.ps1 add:
. C:\path\to\Pee-Object.ps1
Write-Host
(on which this command is built) still isn't the best idea in production PowerShell or reusable modules. As of PowerShell 5, it CAN be redirected to not display console messages, but you still have the chance for console/output race conditions.