Question:
I’d like to get some help putting together a PowerShell script that starts two processes (in my case two servers that monitor file changes and report, writing stuff to stdout continuously) and mixes their respective outputs into one terminal window?
I am not interested in writing into files. I want to monitor the two intertwined output streams in the same PowerShell terminal window. I know that at points there will be mix of “unfinished” chunks from one process intertwined with the same from the other etc., but I don’t mind that.
Image these two scripts as inputs:
left.ps1
1 2 3 4 5 6 7 8 9 10 11 12 |
echo 0 Start-Sleep 1 echo 1 Start-Sleep 2 echo 2 Start-Sleep 3 echo 3 Start-Sleep 4 echo 4 Start-Sleep 5 echo "done numbers" |
right.ps1
1 2 3 4 5 6 7 8 9 10 11 12 |
echo A Start-Sleep 1 echo B Start-Sleep 2 echo C Start-Sleep 3 echo D Start-Sleep 4 echo E Start-Sleep 5 echo "done letters" |
The desired outputs is:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
0 A 1 B 2 C 3 D 4 E done numbers done letters |
It would be nice having the process die when both of the started processes die and also to kill the started processes upon ^C
, but neither is required.
I am using Windows so a Windows-only answer is fine. I do not want to use tmux
or screen
if possible.
(I’m not interested in Cygwin and also I do not want two columns but a mix of stdouts.)
A WSL solution would also be interesting.
Answer:
You can use Start-Process
cmdlet with -NoNewWindow
switch to start new independent console application attached to the same console window:
1 2 3 |
Start-Process -NoNewWindow process1 arguments1 Start-Process -NoNewWindow process2 arguments2 |
If you want to start PowerShell script this way, then you need to start new PowerShell process for it:
1 2 3 |
Start-Process -NoNewWindow powershell '-File script1.ps1' Start-Process -NoNewWindow powershell '-File script2.ps1' |