≡ Menu

Close applications gracefully using powershell

By saying “Graceful” close, I mean allowing the users to save any un saved data before exiting the application. It is equivalent to clicking on “CLOSE” button at the right top corner. The advantage with this method when compared “stop-process” is, it allows the user to save data and hence there is no data lose.

This option came handy to me several time.

Code:

Get-Process notepad | % { $_.CloseMainWindow() }

The above code will get list of  notepad processes running in your PC and prompts you to save data for the processes which have unsaved content. The notepad processes which don’t have anything to save will get closed normally.

Hope this helps you…

{ 7 comments… add one }
  • Noel Burgess May 5, 2011, 3:29 pm

    I don’t know the first thing about Powershell, although someday I hope to learn it. In the meantime, I’ve been looking for a script that will
    1. pass a ‘close’ command to a running program (wlmail.exe) (the program’s own close routine first destroys the UI, then conducts some internal housekeeping before the process ends);
    2. display a message box saying that WLMail is saving your data;
    3. remove that message box when the process ends normally.
    The script would ideally be invoked by, say, clicking a button on the desktop that is created when the wlmail.exe process starts.
    Is this feasible, do you think?

  • vuthu nkiuna September 6, 2012, 12:16 pm

    ITS NICE TO USE

  • RiOt June 12, 2013, 9:07 pm

    Ok Nice when in command prompt but it does not work when you schedule the task in windows task scheduler
    % { $_.CloseMainWindow() } return false in this case and the application is stil open.

    Any idea about that problem?
    Best regards,
    RiOt

    • Surender August 6, 2014, 1:58 pm

      Hi Riot,
      You can use:
      Get-Process anything | ? { $_.CloseMainWindow() | Out-Null }

    • Steve July 22, 2016, 1:07 am

      I believe the reason it doesn’t work for you is because the scheduled task runs under one user account and the task you are trying to close is running under a different user account whereas when you are running from the command prompt both the command prompt and the task you are trying to close are running under the same user account. I have the same problem and have not (yet) found a solution for it.

  • MIchael January 22, 2016, 5:41 pm

    Thank you for this!

    I need it for Firefox on an keyboardless Infoscreen. If you kill the firefox-process rather than shutting it down gracefully, next time firefox comes up with an apology because it thinks it has crashed. And there is nobody at the infoboard to enter “ok”….

Leave a Comment