Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Powershell from exiting

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 want a script command(s) that executes then leaves the shell open.

like image 983
rhavin Avatar asked Feb 20 '12 14:02

rhavin


People also ask

How do I stop PowerShell from opening and closing?

So, let's check out how you can disable the PowerShell Startup status on the Task Manager: Press Ctrl + Shift + Esc to open the Task Manager. Navigate to the Startup tab. Right-click on the Windows PowerShell option and select Disable.

How do I keep a PowerShell script running?

There are a couple of options to run a PowerShell script. The most convenient way is to simply right-click the file and choose Run with PowerShell. But this method comes with a downside. By default, most PowerShell scripts will close the PowerShell window automatically when the script is done.

How do I keep the PowerShell window open?

Global Fix: Change your registry key by adding the -NoExit switch to always leave the PowerShell Console window open after the script finishes running.


2 Answers

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.

like image 105
deadlydog Avatar answered Oct 10 '22 05:10

deadlydog


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' 
like image 43
Roman Kuzmin Avatar answered Oct 10 '22 06:10

Roman Kuzmin