diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 3451fd96ac2..437ecea406c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -1247,7 +1247,16 @@ protected override void ProcessRecord() StopDependentService(process); } - if (!process.HasExited) + // If the process has not exited and we are not Forcing, attempt close request + if (!process.HasExited && !Force) + { + _closeRequest = process.CloseMainWindow(); + } + + // If CloseMainWindow request was not successful, or unsupported, fallback to Kill + // In some cases, this can have the affect that running Stop-Process without Force + /// twice would revert to Kill() + if (!_closeRequest) { process.Kill(); } @@ -1297,6 +1306,11 @@ protected override void EndProcessing() /// private bool _shouldKillCurrentProcess; + /// + /// Was the current process successfully sent a Close Request, or should it be Killed. + /// + private bool _closeRequest; + /// /// Boolean variables to display the warning using ShouldContinue. ///