Here is what I have tried:
Get-Process | Where-Object {$_.Path -eq "C:\path\stream1.bat"} | Stop-Process -Force
Start-Sleep -Seconds 5
Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"C:\path\stream1.bat`""
The Start-Process command works perfectly, however the Stop-Process command does nothing.
One issue with your current code is that
.Path(in reality this property is.MainModule.FileName) will be the path tocmd.exenot the.batfile itself. What you can do in this case is queryWin32_Processand filter by theCommandLineproperty where it contains the path of your.bat. This solves one of the problems.The next problem will be to find all sub processes that your
.batfile could've started, these need to be terminated first before terminatingcmd.exeitself. For this, again, you can queryWin32_Processto find all processes having theParentProcessIdequal to the process id of yourcmd.exeprocess.