Capture Windows Form close event in Powershell
The event to capture is Closing
event of the form which has an event argument which allows you to cancel the event. To learn how to use event args in PowerShell, you may want to take a look at Windows Forms Controls Events in PowerShell - Use Sender and EventArgs.
Example
Add-Type -AssemblyName System.Windows.Forms$form = New-Object System.Windows.Forms.Form$form.Text ="Test"$form.Add_Closing({param($sender,$e) $result = [System.Windows.Forms.MessageBox]::Show(` "Are you sure you want to exit?", ` "Close", [System.Windows.Forms.MessageBoxButtons]::YesNoCancel) if ($result -ne [System.Windows.Forms.DialogResult]::Yes) { $e.Cancel= $true }})$form.ShowDialog() | Out-Null$form.Dispose()