Stop Powershell from exiting

后端 未结 8 1898
傲寒
傲寒 2021-02-05 06:01

I know that there is that little -noexit switch for PowerShell.

Is there anyway of staying in the shell without using that switch?

In other words, I

相关标签:
8条回答
  • 2021-02-05 06:40

    I'm not aware of a command you could run in the script that would prevent the shell from exiting if it had not been invoked using the -noexit command.

    I typically use Read-Host "Press ENTER to continue" at the end if I don't want the shell to close. However this won't prevent the shell from closing after you press enter if it had not been started with -noexit.

    0 讨论(0)
  • 2021-02-05 06:44

    This script will not exit if you run it without arguments, e.g. by double-clicking on it:

    param($Work)
    
    # restart PowerShell with -noexit, the same script, and 1
    if (!$Work) {
        powershell -noexit -file $MyInvocation.MyCommand.Path 1
        return
    }
    
    # now the script does something
    # this script just outputs this:
    'I am not exiting'
    
    0 讨论(0)
  • 2021-02-05 06:44

    Have you tried

    $host.enternestedprompt()
    

    That will stop execution and drop them to a nested prompt. When they exit that prompt, then the script will finish and the window will close.

    0 讨论(0)
  • 2021-02-05 06:46

    The while loop at the end of this trivial script prompts me for a command and executes it. It runs with the environment of the script, so it is possible to check the values of variables. Entering "exit" terminates the loop when it is executed.

    # Do something.
    
    cd D:\temp
    $current_directory = $(pwd)
    dir
    
    write-host  # Just add a little space after the dir
    
    # Stay in the shell and execute commands.
    
    while ($TRUE) {
      $cmd = Read-Host "PS $(pwd)"
      if ($cmd[0] -eq '"') { iex "& $cmd" } 
        else { iex $cmd }
    }
    

    I'm sure that others will be able to share some refinements, but this is a start. Regards.

    0 讨论(0)
  • 2021-02-05 06:52

    You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.

    1. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
    2. Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
    3. Global Fix: Change your registry key to always leave the PowerShell Console window open after the script finishes running.

    See my blog for more information on which registry keys to modify.

    Sounds like you are looking for option #1 or #3.

    0 讨论(0)
  • 2021-02-05 06:53

    Not to revive a 6-year-old thread or anything, but it should be noted to anyone reading that you can end your script with

    Stop-Process -processname regedit
    

    if you have the registry tweak (global fix) enabled and actually want to run an automatically-closing script.

    0 讨论(0)
提交回复
热议问题