How to catch console close event in PowerShell?

Question:

How do I catch the console close event in PowerShell?

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?

Example:

Answer:

In PowerShell use the engine event PowerShell.Exiting and specify a script blocks which process it.

Here is the example:

Source:

How to catch console close event in PowerShell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply