title: How to catch console close event in PowerShell?
body: <p>How do I catch the console close event in PowerShell?  </p>
  
  <p>I've tried adding a console control handler, and it works fine for CMD, but not in PowerShell, is there a different way in PowerShell?</p>
  
  <p>Example:</p>
  
  <pre><code>namespace Test_ConCtrl {
      class Program {
          public enum CtrlTypes : uint { CTRL_C = 0, ... }
  
          public delegate Boolean ConsoleCtrl_Delegate(CtrlTypes CtrlType);
  
          [DllImport("kernel32.dll")]
          static extern bool SetConsoleCtrlHandler(
              ConsoleCtrl_Delegate HandlerRoutine, bool Add);
  
          public static Boolean My_CtrlHandler(CtrlTypes inConType) {
              switch(inConType) { ... }
          }
  
          static void Add_Handler() {
              ConsoleCtrl_Delegate myHandler = My_CtrlHandler;
              SetConsoleCtrlHandler(myHandler, true);
          }
  
          ...
  </code></pre>