How Can I Give a User a Yes/No Prompt?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script which performs some file management tasks, and then should ask the user whether or not they want to delete a set of files. How do I ask a user a Yes or No question like this?

-- SE, Casper, WY

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SE. There are a couple different ways that you can do this, but because you’re dealing with users the best way is probably to just pop up a message box and let them click Yes or No. How hard is that? Hey, it’s no harder than these few lines of code:

intAnswer = _
Msgbox("Do you want to delete these files?", _
vbYesNo, "Delete Files")

If intAnswer = vbYes Then
Msgbox "You answered yes."
Msgbox "You answered no."
End If

All we’re doing here is creating a message box that features the following:

The message to be displayed.

In this example, the message is Do you want to delete these files?

A Yes button and a No button.

This is specified by the VBScript constant vbYesNo. There are other values that can give you different message box configurations; for example, you could have a message box with buttons labeled Abort, Retry, and Ignore. Who could forget good old Abort, Retry, and Ignore?

The message box caption.

In this example, the caption is simply Delete Files.

The message box will sit there until the user clicks a button. And how do we know which button was clicked? Well, the results of the button click are stored in the variable intAnswer. All we have to do is check the value of intAnswer. If intAnswer is equal to another VBScript constant (vbYes) then we know the user must have clicked yes; if it’s equal to anything else, then they must have clicked No. (After all, we only have two buttons, so if they didn’t click Yes they had to have clicked No.)

For more information about the MsgBox function, see the VBScript documentation on MSDN.

Comments (17)

  1. mredwilson says:

    @cj Yes I care and I read the comments. Glad it helped. Have an awesome day.

  2. Wessun007 says:

    HI there, although this script is in VB would you have a solution in Powershell aswell?

  3. CJ says:

    This helped me alot…. not that anyone is going to care or let alone see this message…. lol

  4. PH says:

    I am using Visual Basic and it's saying "end of statement expected" in the error box…. Help…


  5. JC says:


    I am trying to create a script that enables a user to restart a locked down Windows 7 (Ent) workstation … a with a trigger yes/no message ("Are you sure you want to restart?" = click yes or no button to proceed/abort the shutdown).

    Can someone advise how I can do this?

    The reason for this is because we have a few machines as meeting room/presentation computers that staff keep shutting down at the end of the meetings – and we want to prevent this by way of GPO blocking this and then scripting the ability to restart and elevating the script with an account that has this privilege.



  6. please says:

    necesito agregar una condicion AL apretar los botones

    set intro3 = createobject("scripting.filesystemobject")

    set archivo3 = intro3.opentextfile ("E:programa adivinadorcuadro0.txt",1)

    msgbox archivo3.readall, vbyesno

    if =vbyes then

    msgbox "+"

    else =vbno

    msgbox "-"

    end if

  7. Nissi says:

    Hey, Scripting Guy!

    i have a question ,how to access/read data from excell worksheet using "Range" ?

    i tired the below

    Set ObjExcel = Creatobjet(" Excel.Application )


     MsgBox " value of cell A6 is " & O bjExcel.Range("A6")

    but i get some error saying "invalid character"

    plz clarify my doubt



  8. Citezyne says:

    What if you wanted to ask a User if the details entered are correct? for example i have VBS that asks for the computer name and asks the user to enter there name then if the user clicks yes an email will be sent to me with these details if the user clicks no then it takes them back to the start of the script to reenter the details.

  9. su says:

    its shoeing error "variable is undefined" 'intAnswer in runtime

    plz help

  10. Anonymous says:

    Hello, I want to incorporate the above code into a batch file, can you suggest a solution? Thank you A

  11. Kaveri says:

    How can we copy some properties from one shared repository(1.tsr) to another shared repository (2.tsr)

  12. kaveri says:

    whether we stop the QTP tool at the end of the day in the office?or not.

  13. Maxwell says:

    Hi, I need a script to automatically click ‘yes’ to one of these pop up boxes. For example, I have a batch file to run a program a bunch of times. But at the end the program pops up with ‘would you like to close this window’. So the batch file doesn’t
    really work as I still have to click ‘yes’ after every program run. Is there anyway I can add this to a batch file script?

  14. ScriptFollower23 says:

    Just and Example Script I have typed up so far…I keep getting errors, and I want it where when the person hits abort then it functins the vbAbort I have put…vbCancel then It will close the window
    vbRetry then it will bring up the box again with vbYesNo
    and have vbYes as what I have put, and with vbNo set to close the box…

    intAnswer =
    Msgbox("do you like pizza?")
    If intAnswer = vbAbort Then
    Msgbox "why would you not say anything?"
    if intAnswer = vbRetry Then
    MsgBox("do you like Pizza?")
    if intAnswer = intAnswer = vbYes Then
    Msgbox "I like Pizza too!"

  15. lathan says:

    awesome! so simple

  16. Manank says:

    Thanx a lot for this script

Skip to main content