Stop Powershell from exiting

后端 未结 8 1875
傲寒
傲寒 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: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.

提交回复
热议问题