I\'m currently working on a little PowerShell Script to reset a users Outlook Profile on a remote machine.
Before the necessary profile stuff can be done I want to chec
To gracefully close a Windows application, send the app a WM_CLOSE message. This should work as long as the user doesn't have modified state that will cause the app to pop UI asking whether changes should be saved or not. Here's an example that shows how to close a notepad app - again as long as there are no unsaved changes.
$src = @'
using System;
using System.Runtime.InteropServices;
public static class Win32 {
public static uint WM_CLOSE = 0x10;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
'@
Add-Type -TypeDefinition $src
$zero = [IntPtr]::Zero
$p = @(Get-Process Notepad)[0]
[Win32]::SendMessage($p.MainWindowHandle, [Win32]::WM_CLOSE, $zero, $zero) > $null