Exit a Powershell Script



  • Hi Everyone

    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.
    Any ideas?

    Answers are greatly appreciated!



  • The shortcut contains the following:

    powershell.exe -noexit -ExecutionPolicy Bypass -File "path to .ps1 file"
    

    @TechWiz & @Knaper-Yaden any ideas?



  • 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…


Log in to reply
 

Suggested Topics

  • 2
  • 25

3
Online

364
Users

278
Topics

1.4k
Posts

Copyright © 2020 | info@shutfim.com