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.
-
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.Vesper– Vesper2015-06-29 14:31:45 +00:00Commented Jun 29, 2015 at 14:31
-
1Downvote for the dead link!aschipfl– aschipfl2019-01-23 09:52:58 +00:00Commented Jan 23, 2019 at 9:52
Add a comment
|
3 Answers
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; }
3 Comments
XAMPPRocky
Are you sure that is correct? When entered I receive the following error.
Register-EngineEvent PowerShell.Exiting †<<<< “Action { is missing the terminator: ".Avshalom
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
XAMPPRocky
I have no errors now, but nothing is happening on exit.
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
XAMPPRocky
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.Avshalom
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
XAMPPRocky
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.
Avshalom
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
XAMPPRocky
I do not see the event as registered. I am running as Admin.
|
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
seamlik
Tested on PowerShell 7.0.0, finally block does not run.