svandragt
svandragt

Reputation: 1720

Powershell script “on exit” event?

Instead of calling a function at the end of all scripts to perform cleanup tasks, I'm looking to register for an 'on return' event for when the script (not the PowerShell session) is finished.

I can't find a list of built-in powershell events or an alternative solution.

Upvotes: 3

Views: 2676

Answers (1)

briantist
briantist

Reputation: 47792

@Vesper wrote it as a comment, but a try/finally block is definitely what I would suggest for this:

try {
    # some code
} finally {
    # this gets executed even if the code in the try block throws an exception
}

Upvotes: 5

Related Questions