How to start and stop processes in PowerShell?

ppiotrek picture ppiotrek · Nov 24, 2015 · Viewed 8k times · Source

This should work fine in PowerShell older than 3. I need to run two processes: wuapp.exe and desk.cpl with ScreenSaver's tab. The problem I have is that once I start wuapp.exe the process name shows up in Task Manager as explorer.exe - the current code works for wuapp.exe but not on every machine. The other thing is when I kill rundll32 other apps will close also. What do you suggest?

$wshell = New-Object -ComObject Wscript.Shell
Start-Process  wuapp.exe
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}
Start-Process rundll32 desk.cpl,InstallScreenSaver
Stop-Process -ProcessName rundll32* -Force

Answer

Mathias R. Jessen picture Mathias R. Jessen · Nov 24, 2015

I'm not entirely sure what you mean by wuapp.exe showing up as explorer.exe, but keeping track of the rundll32 process is fairly straightforward.

Use Start-Process with the PassThru parameter to have it return the process it creates:

$ScreenSaverCpl = Start-Process rundll32 desk.cpl,InstallScreenSaver -PassThru
# Call Stop-Process when no longer needed:
Stop-Process $ScreenSaverCpl