One somewhat messy way I can think of is to use one of the ported tee
programs, save to a temporary file and then test the file with find
. However, the use of a temporary file may be undesirable.
If PowerShell is an option, it actually has a Tee-Output
cmdlet. It's not quite as direct as the bash example, but it does have a -Variable
option to save the output into a variable, which can then be searched:
# save result in $LastOutput and also display it to the console echo "some text" | Tee-Output -Variable LastOutput # search $LastOutput for a pattern, using Select-String # instead of find to keep it within PowerShell $Result = $LastOutput | Select-String -Quiet "text to find" # $Result should contain either true or false now # this is the equivalent of batch "if errorlevel 1" if ($Result -eq $True) { # the string exists in the output }
To answer the more general question, it is also possible to pipe the variable into any other program, which will then set $LastExitCode
. As a one-liner that can be called from the basic command line: powershell -c "echo text | Tee-Object -Variable Result; $Result | foo"