Closing a Program Gracefully

by Mar 3, 2010

When you use Stop-Process to kill a program, it will stop instantaneously. The user will get no chance to save unsaved documents:

Get-Process Notepad | Stop-Process

Try a more graceful way by using an internal .NET method, which then acts as if someone closed the program window. If the content has not been saved, a dialog opens and asks for a choice:

Get-Process Notepad |
Foreach-Object { $_.CloseMainWindow() | Out-Null }

Twitter This Tip! ReTweet this Tip!