Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I launch a prompt in PowerShell where a user can give some input. But I would like to catch the action when the user hits "Cancel".
Is there a way to detect when users hits "Cancel" in a prompt window in PowerShell? Or is this option not build in in PowerShell?

share|improve this question

Edit: Actually i found a similar (possibly helpful) topic in here.

Please refer to this topic on technet.

The explanation that you might find useful:

Speaking of OK and Cancel buttons, that’s our next step; this block of code adds a button labeled OK to our form:

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

To add a button, we first create an instance of the System.Windows.Forms.Button class. (This, by the way, is a standard theme when working with forms: each time you add a new control you’ll typically create a new instance of a .NET Framework class.) We next specify a value for the Location property (in this case our button is located 75 pixels from the left side of the form and 120 pixels down from the top of the form); then assign values to the Size and Text properties. After that we use this line of code to indicate what should happen when the user clicks this button:


Look familiar? It should; these are exactly the same things we want to happen if the user presses the ENTER button: we want to assign the value in our text box to $x, and we want to close the form. Simple, huh? From there we then use the Add method to add the button to the form:


And then we repeat the process to add the Cancel button as well.

We can now type anything we want in the text box and then press ENTER (or click OK). When we do so, the form will disappear and the value we entered in the text box will be stored in the variable $x. (Something we verify by echoing back the value of $x with our very last line of code.) Alternatively, we could press ESC or click Cancel and the form will disappear without changing the value of $x.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.