tags:

views:

85

answers:

2

I have to stop a browser from a PowerShell script, which I do by piping it into

Stop-Process -Force

However, this is very abrupt. When the browser is restarted, it detects that it didn't shut down cleanly, and tries to restart the previous session. Is there some way I can tell it to shut itself down gracefully? ("There are two ways we can do this ...")

+4  A: 

Try this to simulate the user closing the app:

(Get-Process -Id 10024).CloseMainWindow()
Keith Hill
+4  A: 

Keith Hill has already proposed to use CloseMainWindow(). But it is only an invitation to close, some user interaction still might be needed, for example an application may show some dialogs to save something and etc. If a calling script really expects a process to be exited, I use this pattern:

# close the window and wait for exit
$_ = Get-Process -Id 12345
[void]$_.CloseMainWindow()
if (!$_.HasExited) {
    Write-Host "Waiting for exit of Pid=$($_.Id)..."
    $_.WaitForExit()
}
Roman Kuzmin
Nice build! Thanks.
Keith Hill