7

How would I run a powershell script that would run on either, the exit command, or the shell closing. I would want this script to run on every shell close not just for one shell.

Script

2
  • Check this question How to get an Unix like atexit in Powershell? Although to make this run at every shell, you need it to include the script listed in the answer, probably by auto-importing a module. I doubt that this functionality is not considered a security violation. Commented Jun 29, 2015 at 14:31
  • 1
    Downvote for the dead link! Commented Jan 23, 2019 at 9:52

3 Answers 3

11

Using Register-EngineEvent you can do this:

Register-EngineEvent PowerShell.Exiting –Action { 'Type your Code' }

# Alternatively with hiding the event: 
Register-EngineEvent PowerShell.Exiting -SupportEvent –Action { yourExitFunction; }
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure that is correct? When entered I receive the following error. Register-EngineEvent PowerShell.Exiting †<<<< “Action { is missing the terminator: ".
Sure, ran this now on PS v2 and v3, works fine: Register-EngineEvent PowerShell.Exiting -Action { New-Item c:\Test.txt -Type File } show the command you ran and the error
I have no errors now, but nothing is happening on exit.
2

Get it out of the function and run it in the scriptblock, like this:

Register-EngineEvent PowerShell.Exiting –Action {

$foo = @"
 .d8888b.  8888888888 8888888888      Y88b   d88P  .d88888b.  888     888
d88P  Y88b 888        888              Y88b d88P  d88P" "Y88b 888     888
 "Y888b.   8888888    8888888            Y888P    888     888 888     888
    "Y88b. 888        888                 888     888     888 888     888
      "888 888        888                 888     888     888 888     888
Y88b  d88P 888        888                 888     Y88b.  d88P Y88b. .d88P
 "Y8888P"  8888888888 8888888888          888      "Y88888P"   "Y88888P"
 .d8888b.  8888888b.     d8888  .d8888b.  8888888888
d88p  Y88b 888   Y88b   d88888 d88P  Y88b 888
 "Y888b.   888   d88P d88P 888 888        8888888
    "Y88b. 8888888P" d88P  888 888        888
      "888 888      d88P   888 888    888 888
Y88b  d88P 888     d8888888888 Y88b  d88P 888
 "Y8888P"  888    d88P     888  "Y8888P"  8888888888
 .d8888b.   .d88888b.  888       888 888888b.    .d88888b. Y88b   d88P
d88P  Y88b d88P" "Y88b 888   o   888 888  "88b  d88P" "Y88b Y88b d88P
888        888     888 888 d888b 888 8888888K.  888     888   Y888P
888        888     888 8888888888888 888  "Y88b 888     888    888
888    888 888     888 88888P Y88888 888    888 888     888    888
Y88b  d88P Y88b. .d88P 8888P   Y8888 888   d88P Y88b. .d88P    888
 "Y8888P"   "Y88888P"  888P     Y888 8888888P"   "Y88888P"     888
"@
 $file="c:\.seeyouspacecowboy"
 $foo | out-file $file
 $colour=("red","yellow","darkyellow","green","cyan","darkcyan","darkmagenta")
 [int]$num=-1
 $info = (get-content $file)
 Write-Host ""
 foreach($i in $info) 
    {
     [int]$perspective=($num / 3)
     write-host $i -foregroundcolor $colour[$perspective]
     $num++   
    }
 Write-Host ""

 }

*of course, make sure you are with admin privilege as it write to c:\

9 Comments

Nope, still getting errors. Unexpected token 'Y88b.' in expression or statement. At C:\Dev\spacecowboy.ps1:7 char:11 also please delete this question, and edit it into your other question.
for the test, don't run it as a ps1 file, just start powershell as administrator and paste the code, type exit, and check the c drive, i test this with PS2 and 3 works great
This doesn't work. There is no errors, or output. On top this this wouldn't be suitable if I had to paste the code in everytime I started the powershell.
of course not, but just for the test, do you see message that the event registerd? do you run powershell as admin? also, you can add this to powershell profile so it will run automatically on every powershell start
I do not see the event as registered. I am running as Admin.
|
2

Another option is a try-finally block, which will execute the finally block on error or ctrl-c:

try {
    # Main code
} finally {
    # Code to run on exit
}

1 Comment

Tested on PowerShell 7.0.0, finally block does not run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.