Prompt user to continue or stop the execution of powershell script

This topic contains 4 replies, has 3 voices, and was last updated by  Vern Anderson 4 years, 9 months ago.

  • Author
  • #10335



    I'm trying to put a condition in my script to prompt the user to continue or stop the execution.
    I have used read-host cmdlet, it asks for user input and two tabs 'Ok' and 'Cancel'. If i press Ok the script execution continues and if i click on Cancel, execution stops at that point but i believe that i not the purpose of read-host ( i expects an input from user ). Is there any cmdlet or procedure to prompt the user to just stop or continue the execution of powershell script.

    Please let me know , thanks in advance

  • #10336

    Richard Siddaway

    Does the cmdlet you using have a -Confirm parameter?

    if so you could use that

    It will ask for confirmation to proceed

    However it only works for a cmdlet & won't break you out of the script
    if you are responding to an error look at the Inquire option on -ErrorAction parameter

  • #10347

    Vern Anderson

    If the user will be running from powershell version 3 or greater the pause command is back and it works. However it didn't exist in PowerShell Version 1 or 2

    if ($PSVersionTable.PSVersion.Major -ge 3) {Get-Command Pause}

    also. . .


  • #10349

    Richard Siddaway

    You might want to look at the definition of the pause command

    Read-Host 'Press Enter to continue...' | Out-Null

    Its not really that different to what you were doing.

    The more things change the more they stay the same

  • #10384

    Vern Anderson

    Thanks Richard I will look again. Last time I tried using that method to pause a script in PS v2 it continued after a few seconds.

You must be logged in to reply to this topic.