locked
powershell script runs then exits RRS feed

  • Question

  • I have a few powershell scripts I like to run by simply right clicking and choosing 'run with powershell'.  It does but exits as soon as I run them.  I would like for the screen to stay open so I can view the results.  Below is an example.

    Import-Module

     

     

    ActiveDirectory
    $User = Read-Host "Username?"
    Get-ADUser $User -properties PasswordLastSet |

    Format-List

    Monday, December 12, 2011 5:17 PM

Answers

  • Hi,

    Or just use Read-Host "Press any key to close..." after all:

    Import-Module ActiveDirectory
    $User = Read-Host "Username?"
    Get-ADUser $User -properties PasswordLastSet | Format-List
    Read-Host "Press any key to close..."
    

    Tuesday, December 13, 2011 1:50 PM
  • Open a Powershell console, and run the script from there.  You will find the Powershell shortcuts under All Programs/Accessories/Windows Powershell.

    To run a script from the console, type:

    <path to script>\myScript.ps1 <Enter>


    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Monday, December 12, 2011 5:28 PM
  • You may consider hacking the Registry key to include some sort of entry for the Powershell.exe to use the -NoExit switch.  This will prevent the shell from closing, although, I have to admit, I have never found 100% effective approach for this.  Some other ways I have done it is to write my own shortcut and pass my own commands.  I did this here:

    http://learningpcs.blogspot.com/2010/08/powershell-run-script-from-shortcut.html

    Also, you can manipulate the context menu entries through the registry.  I have done that here:

    http://learningpcs.blogspot.com/search?q=powershell+context+menu+registry

    Tuesday, December 13, 2011 6:21 AM
  • And, if need be, you can create clickable scripts with the Read-Host at
    the end that invoke the actual scripts coded without the Read-Host
    interaction at the end.
     
    Tuesday, December 13, 2011 3:13 PM
  • You can also use the [System.Windows.Forms.MessageBox]::Show() static method to display a modal dialog instead of displaying the information in the console. Not that I'm advocating modal dialog boxes exactly ... :)

    http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

    Hope this helps.


    If this post was helpful, please click the little "Vote as Helpful" button :)

    Trevor Sullivan
    Trevor Sullivan's Tech Room
    Twitter Profile
    Tuesday, December 13, 2011 5:07 PM

All replies

  • Open a Powershell console, and run the script from there.  You will find the Powershell shortcuts under All Programs/Accessories/Windows Powershell.

    To run a script from the console, type:

    <path to script>\myScript.ps1 <Enter>


    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Monday, December 12, 2011 5:28 PM
  • You may consider hacking the Registry key to include some sort of entry for the Powershell.exe to use the -NoExit switch.  This will prevent the shell from closing, although, I have to admit, I have never found 100% effective approach for this.  Some other ways I have done it is to write my own shortcut and pass my own commands.  I did this here:

    http://learningpcs.blogspot.com/2010/08/powershell-run-script-from-shortcut.html

    Also, you can manipulate the context menu entries through the registry.  I have done that here:

    http://learningpcs.blogspot.com/search?q=powershell+context+menu+registry

    Tuesday, December 13, 2011 6:21 AM
  • Hi,

    Or just use Read-Host "Press any key to close..." after all:

    Import-Module ActiveDirectory
    $User = Read-Host "Username?"
    Get-ADUser $User -properties PasswordLastSet | Format-List
    Read-Host "Press any key to close..."
    

    Tuesday, December 13, 2011 1:50 PM
  • And, if need be, you can create clickable scripts with the Read-Host at
    the end that invoke the actual scripts coded without the Read-Host
    interaction at the end.
     
    Tuesday, December 13, 2011 3:13 PM
  • You can also use the [System.Windows.Forms.MessageBox]::Show() static method to display a modal dialog instead of displaying the information in the console. Not that I'm advocating modal dialog boxes exactly ... :)

    http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

    Hope this helps.


    If this post was helpful, please click the little "Vote as Helpful" button :)

    Trevor Sullivan
    Trevor Sullivan's Tech Room
    Twitter Profile
    Tuesday, December 13, 2011 5:07 PM
  • You basically have 3 options 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.
    2. Per-script fix: Add a prompt for input to the end of your script file (i.e. Read-Host).
    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 the registry keys to modify.


    - Dan - "Can't never could do anything"




    • Edited by deadlydog Monday, July 7, 2014 10:28 PM
    Monday, July 7, 2014 10:10 PM
  • Hi All,

    Found another way, just adding it to the list for others to use.

    #Call all three scripts parallelly and keep their windows open after execution.
    
    Start-Process powershell.exe  -ArgumentList "-NoExit .\Script2.ps1"
    Start-Process powershell.exe  -ArgumentList "-NoExit .\Script3.ps1"
    Start-Process powershell.exe  -ArgumentList "-NoExit .\Script4.ps1"


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Thursday, September 24, 2015 5:26 AM