Exit a Powershell Script
I wrote the following PS script:
Enable-NetFirewallRule -DisplayName "Network Discovery"; exit
If you paste that code to a PS console with, run as an admin it will execute the command and exit (close the window) powershell.
When I run this script through a shortcut the window stays open and it doesn’t exit. The shortcut runs powershell.exe as admin and then the shortcut points to the location of the script with some parameters beforehand.
The behaviour is specific to the exit command. If I’m running let’s say “ipconfig” after the semicolon it runs just fine.
Answers are greatly appreciated!
Knaper Yaden last edited by Knaper Yaden
The behavior makes sense, you can’t tell a Script to Exit something that it is not itself - the Powershell window opened by the shortcut - if you just run the ps1 file from the shortcut it will Exit, but then you can’t put your arguments, are they needed?)
I’ve seen someone using a workaround to shut the PowerShell window from within the script using ($PID is a variable for the current PowerShell session):
stop-process -Id $PID
@Knaper-Yaden Thanks for you reply. I just realised that I’ve added the -noexit parameter by mistake… I think that is why it ignores exit…