Introduction: Fun With Easy VBS!!!

A Beginners guide to fun, and easy to do visual basic script. VSB may seem hard to some, but in fact it is really easy. Windows can even run it without a compiler! If you know javascript, it should be really easy for you.

Step 1: Basic Message Boxes

Ok so lets go over the Basics.

Have you ever seen a message box like the one below,
you probaly have. These are used to alert users of whatever you want to say.

You can create your own by using the Function Msgbox

So the code to a message box that says hi would look like this,

Msgbox("hi")

Alternately you can make an unkillable message box by making a simple loop by using the Do, Loop commands.

Now that we covered message boxes, lets move to more advanced message boxes.

Step 2: Games and Stuff

If you ever wonder how to make text games than look no further. I might be wrong, but I believe that they used vbs. Now a lot of vb scripts use a compiler, but windows doesn't need one. Just type cscript before the file path, now if you know how to make bat files, then you can automate this process, but we wont get into that in this instructable.

Here is a little game i made

Option Explicit

Dim Answer

'types the question

Wscript.StdOut.Write "Nice Day Isin't it? "
answer = Wscript.StdIn.ReadLine

'if you type yes t

If answer="yes" Then
Wscript.echo ("I hope it stays this way")

'If you type no

ElseIf answer="no" Then

Wscript.echo ("I hope it gets better")

'anything else

Else

Wscript.Echo ("GoodDay to you")

'end

End If

now basically save this as .vbs and use the method before to run it.

Step 3: The Auto Typer

This code it to type something over and over again. I don't know how it is useful, but if you run it, you'll have to kill it in the task list

set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"

do

shellobj.sendkeys "Y"
wscript.sleep 200
Shellobj.sendkeys "o "
wscript.sleep 200

loop

Step 4: The Matirx Has You

Ever wondered how to make you pc talk to you?

well copy this



option explicit
On Error Resume Next

' declare variables
dim wshshell, title, mystring, length, position, tmp, loopA, loopB, linesToWrite, columns, objUser, objSysInfo, myName, myNameLength, col1, col2, col3, col4, col5, maximum, elements

set wshshell = wscript.createobject("wscript.shell")
title = "the matrix"

wshshell.SendKeys "{ALT}"+"{ENTER}"

' get user info, used to get the name
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

' ---------------------------
sub waitfor(var)
' set up window, set title, wait for it to complete
tmp = false
do until tmp
wscript.sleep 1000
tmp = wshshell.appactivate(var)
loop
end sub

' ---------------------------
sub matrix(elements)
' print random numbers to make "the matrix" look
' assumes elements is divisible by 5
columns = 5
col1 = true
col2 = true
col3 = true
col4 = true
col5 = true
randomize()
linesToWrite = elements / columns
for loopA = 1 to linesToWrite
for loopB = 1 to columns
Select Case loopB
Case 1
If col1 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 2
If col2 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 3
If col3 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 4
If col4 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
Case 5
If col5 = true Then wshshell.SendKeys Int(Round(rnd())) & "{TAB}" Else wshshell.SendKeys "{TAB}"
End Select
wscript.sleep 5
next
wscript.Echo

' This flip-flops columns to display 20 percent of the time, independently of each other.
maximum = 0.20
If rnd() < maximum Then
If col1 = true Then col1 = false Else col1 = true
End If
If rnd() < maximum Then
If col2 = true Then col2 = false Else col2 = true
End If
If rnd() < maximum Then
If col3 = true Then col3 = false Else col3 = true
End If
If rnd() < maximum Then
If col4 = true Then col4 = false Else col4 = true
End If
If rnd() < maximum Then
If col5 = true Then col5 = false Else col5 = true
End If
next
wscript.Echo
wscript.Echo
end sub

' ---------------------------
sub key(msg)
' do printouts of "the matrix" messages, and erase them
wscript.sleep 1500
length = len(msg)
for position = 1 to length
wshshell.SendKeys mid(msg, position, 1)
wscript.sleep 250
next
wscript.sleep 3000
for position = 1 to length
wshshell.SendKeys "{BACKSPACE}"
wscript.sleep 75
next
end sub

' ---------------------------
' start "main" execution here

waitfor(title)

For loopA = 1 To 4
Select Case loopA
Case 1
mystring = "wake up, Neo..."
Case 2
mystring = "The matrix has you..."
Case 3
mystring = "Follow the white rabbit..."
Case 4
mystring = "Knock knock..."
End Select
' now print out the correct string
key(mystring)
next

'_ _ _ _ _ _ _ _ _ _

wscript.quit()


save it to C:\temp as matrix.vbs

the create the bat files


@echo off
color 0a
title the matrix
set /p matrix= | cscript /nologo

(Replace this with your path to the vbs file)

exit
set /p matrix=


Name that RabbitHole.bat. Then run it!!

this is an cool, but advanced thing to do with vbs

I just put it in because it is awesome

Step 5: End

There you go


The skys the limit to what you can do

Comments

author
VicentVega made it! (author)2015-09-18

okay so i am relatevely new to programming, can someone tell me if this code will be able to work?

Set wshShell =wscript.CreateObject("WScript.Shell")

do

for var = 0 to 5

msgbox("hello")

next

msgbox("Let's have fun;)")

next

wscript.sleep 100

wshshell.sendkeys "{CAPSLOCK}"

wshshell.sendkeys "{NUMLOCK}"

wshshell.sendkeys "{SCROLLLOCK}"

loop

next

Set oWMP = CreateObject("WMPlayer.OCX.7" )

Set colCDROMs = oWMP.cdromCollection

if colCDROMs.Count >= 1 then

do

For i = 0 to colCDROMs.Count - 1

colCDROMs.Item(i).Eject

Next ' cdrom

For i = 0 to colCDROMs.Count - 1

colCDROMs.Item(i).Eject

Next ' cdrom

loop

-----------------------------------------------------------------

how should I save that?

author
ema3277 made it! (author)ema32772017-01-15

This is a virus because Avast deleted it when I clicked on it. :D

author
FirelM made it! (author)FirelM2017-01-17

VBScript isn't a virus. Avast is just choosing it because it THINKS it is.. All of the code is safe. I tested it. Nothing was damaged! :D

author
Kerrkbl123 made it! (author)Kerrkbl1232017-01-11

I haven't tested the cdrom part of the code, as I don't have cd tray but the code isn't spitting any error out so it should work (may need some adjusting for a particular system though)

Enjoy

Lights.PNG
author
JeffreyB35 made it! (author)JeffreyB352015-11-30

random.vbs click the drop down bar that says "Text Document (*txt)" and click "all files" then click save

and if you want to have it work on start up go here https://www.youtube.com/watch?v=c1RbRuCT4R4

author
TrendyG made it! (author)2016-08-11

im very new to coding with vbs so can someone tell me why it says The handle is invalid

Here is my code:

Option Explicit

Dim Answer

Wscript.StdOut.Write "Error Please retype password"

answer = Wscript.StdIn.ReadLine

If answer="2112" Then

Wscript.echo ("logging in")

Wscript.sleep 10

msgbox("Logged in")

Elseif answer="443" Then

Wscript.echo ("That is incorrect")

End if

author
Kerrkbl123 made it! (author)Kerrkbl1232017-01-11

Picture below is code for the password/log on box:

Enjoy

Password.PNG
author
RexfordS made it! (author)2015-01-19

i want to create a riddle box but its seems to skip a part this is the code i used

option explicit
dim main, age

do
main=inputbox("What goes up and never comes down?","Riddle!!","Answer goes here!")

if main = "" then
msgbox"Oh! boy, you are wrong."
elseif main = "age" or main = "Age" then
msgbox"You are right!!!",vbOkonly+vbSystemModal,"Riddle_Answer"
loop until main = "age" or "Age"
elseif main <> "age" then
msgbox"You are so wrong!",vbOKOnly+vbSystemModal,"Wrong_Answer."
else
msgbox"Oh! boy, you are wrong."

end if

author
Kerrkbl123 made it! (author)Kerrkbl1232017-01-10

Its a bit late but here is the code for the riddle box:

Code.PNG
author
Clockworx made it! (author)2016-05-09

Yeah, I just have one problem. How do I get the Message box to appear in a random place?

author
EdS53 made it! (author)2016-02-14

Doesn't work.

Capture.PNGCapture1.PNG
author
MuBoks Stuff made it! (author)2015-09-15

i need help how can i serch an whole pc for an file and if found delete it and replace it with another

author
nathan.nicholson.315 made it! (author)2015-02-11

HI ALL can someone help me write a script that will ask questions like your name surname and than email option

author
nathan.nicholson.315 made it! (author)2015-02-11

HI ALL can someone help me write a script that will ask questions like your name surname and than email option

author
jason.phelps.2112 made it! (author)2015-01-28

---------------------------------------------
Option Explicit
Dim OBJ,RID : Set OBJ=(CreateObject("Wscript.Shell"))
Do : RID=(InputBox("What goes up and never comes down?","Riddle!!","Answer goes here!"))

If(LCase(RID))<>("age") Then
If(RID)=("") Then WScript.Quit
If(RID)=("Answer goes here!") Then WScript.Quit
OBJ.Popup("Oh! boy, you are wrong."),2,"Wrong, try again.",0
Else : OBJ.Popup("You are right."),0,"Riddle Answer.",0
Exit Do
EndIf
Loop : WScript.Quit
---------------------------------------------
note: obj.popup will work compiled, msgbox will not.

author
RexfordS made it! (author)2015-01-19

i want to create a riddle box but its seems to skip a part this is the code i used

option explicit
dim main, age

do
main=inputbox("What goes up and never comes down?","Riddle!!","Answer goes here!")

if main = "" then
msgbox"Oh! boy, you are wrong."
elseif main = "age" or main = "Age" then
msgbox"You are right!!!",vbOkonly+vbSystemModal,"Riddle_Answer"
loop until main = "age" or "Age"
elseif main <> "age" then
msgbox"You are so wrong!",vbOKOnly+vbSystemModal,"Wrong_Answer."
else
msgbox"Oh! boy, you are wrong."

end if

author
NicholasV1 made it! (author)2014-12-31

Can anyone tell me (a n00b) how to make the Red X work on my message box?

author
nicholaslol123 made it! (author)nicholaslol1232015-01-06

well if your using the MsgBox you would want to put a ,0+16, for example A=MsgBox("hello sir",0+16,"computer")

author
HiddenHer0 made it! (author)2014-07-07

hello,

Im trying to make a msgbox script where when you open it it says something like usual, then we you close it it waits a few seconds and opens again. Is this possible? if so can I have some help?

author
BMP1 made it! (author)BMP12014-11-11

do you want the loop to be infinite??? if so, use this:
'---------------------------------------
Set objShell = WScript.CreateObject( "WScript.Shell" )
msg = "what you want the box to say"
wait = 1000 'this is how long it waits before the next text box, 1000 = 1 second

Do
MsgBox(msg)
wscript.sleep wait
Loop
'--------------------------------------

if you don't want it to be infinite:
'--------------------------------------
Set objShell = WScript.CreateObject( "WScript.Shell" )
msg = "what you want the box to say"
wait = 1000 'this is how long it waits before the next text box, 1000 = 1 second
loops = 10 'how many times you want the loop to go


count = 0
Do while count < loops
MsgBox(msg)
wscript.sleep wait
count = count + 1
Loop
'--------------------------------------

author
Riaan111 made it! (author)2011-08-08

Hi

I just want to know how to make a message box do something depending on what option you select (yes,no,cancel)

Say for example I make a message box and a person gives an input, depending on which input it will open another message box eg. "Are you happy" when the person selects the "Yes" option it goes to another message box saying "That's good" and when they say no, something else comes up.

Any help on this would be much appreciated
Thanks in advance

author
mthom41 made it! (author)mthom412012-10-07

0 - ok button only 1 - ok and cancel 2 - abort, retry and ignore 3 - yes no and cancel 4 - yes and no 5 - retry and cancel
                                                      msgbox"Hi",3,"mthom41"
these set button values in a msgbox between quotes. the 3 sets it to yes, no, and cancel.
dim var1  
var1=msgbox ("am i cool?,3,"mthom41")
this creates a variable called var1, then sets its value to a msgbox output.
1 = OK 2 = Cancel 3 = Abort 4 = Retry 5 = Ignore 6 = Yes 7 = No
these are button return values.
now... you need to combine these with an if.. then.. statement and polish it up a little

dim decision
decision=msgbox ("'Sigghhhh...' I should try going somewhere." &vbnewline& "I'll take a step... or i could not, and sit down",3,"Yes=step No=sit and think. Finally, Dialogue!")
if decision=6 then
msgbox "'AAAAHH!!!!' I tripped over a cable!!!",48,"Falling....."
wscript.sleep 4000
msgbox ".............",48, "you are knocked out :("
else if decision=7 then
msgbox"I'll just sit here and wait...zzzzzzz",64,"Hope i don't fall asleep..zzzzzzz"
wscript.sleep 4000
else if decision=2 then
msgbox"You can't avoid it"
end if
end if
end if

this makes a series of boxes... well just look at the pics...



i1.JPGicancel.JPGino.JPGiyes.JPG
author
mthom41 made it! (author)mthom412012-10-07

sorry. pic 1 is the main box, 2 is cancel, 3 is no, and 4 is yes

author
nhadatnamdinh made it! (author)2012-08-07

Set wshShell =wscript.CreateObject("WScript.Shell")
do
wscript.sleep 100
wshshell.sendkeys "{CAPSLOCK}"
wshshell.sendkeys "{NUMLOCK}"
wshshell.sendkeys "{SCROLLLOCK}"
loop

author
DJP4VEP made it! (author)2009-04-21

you arnt actually teaching anyone how to create a script. what is the point in this instructable? im wanting to know what to put at the end of a fake virus script i have made in order to shutdown the computer after 60 secs. please help me

author
animan1 made it! (author)animan12009-06-15

to get a shutdown timed to 60 seconds you mean? are you using batch or vbs?

author
lanky.ninja made it! (author)lanky.ninja2011-07-18

Batch: shutdown -s -t 60 -f
vbs:
dim objshell
set objshell = createobject(wscript.shell)
objshell.run "shutdown -s -t 60 -f"




This is not completely reliable i may be wrong but it is along those lines.

*for more batch shutdown options open cmd and type "shutdown /?"

author
UBERBANKAI made it! (author)2010-04-05

Hey, is there a way to have your computer run like a sound file when you hit yes on a message box?
Say i were to have a message box say: (in text) Hello I am a virus, don't be alarmed.
then it would have just an ok button  then when they press ok it  plays some sound from their computer.

author
Super_Nerd made it! (author)Super_Nerd2011-05-04

yes put the sound file in the same folder as the script. Enter this in the script...

set wshshell = wscript.CreateObject("wscript.shell")

a=msgbox ("Hello I am a virus, don't be alarmed.", 16)

wshshell.run "file.wav"

I assumed the file was a wav file. Where it says file.wav type the full filename like
music.mid or tada.wav

author
17hmr243 made it! (author)2010-09-09

just funny way to shut pc down

so write u own funny mesg inbetween " "
so it could be

onclick=msgbox ("your hard drive has failed please format",2,"ph home to late shutting down")

now u can change to numbers from 1 to 20 to get dif boxes
copy past into notpad save as any thing u whant but must end in .vbs

all so chang the .vbs icons to avi icon and snoopy people will click on it and it will just turn off u pc
google that part im tyed off to bed have fun

onclick=msgbox ("your hard drive has failed please format",2,"com%dot disk format fat32")
onclick=msgbox ("your hard drive is running to fast",13,"com%dot disk format fat32 restart")
onclick=msgbox ("failed please format",14,"com%dot disk format fat32 restart")
onclick=msgbox ("save and exit now",15,"com%dot disk format fat32 restart")
onclick=msgbox ("sending passwords",16,"com%dot disk format fat32 shut down")
onclick=msgbox ("sending bannking details",17,"com%dot disk format fat32 restart")
onclick=msgbox ("sending b.s.b details",18,"com%dot disk format fat32 formating hard drive")
onclick=msgbox ("thank you for your time and $",19,"you have just been hacked")
Set shell = WScript.CreateObject("WScript.Shell")
shell.Run "%windir%\system32\shutdown.exe -s"

author
crjeea made it! (author)crjeea2011-02-21

try using the command at the end instead...

shutdown /s /f /t 00

author
nova made it! (author)2010-04-27

Hey mdkelsh, have u considered batch programming? it is mutch simpler and easier to use.

author
mdkelsh made it! (author)mdkelsh2010-04-27

 Yes, I program in python now, since I consider VBS to be defunct

author
nova made it! (author)nova2010-04-28

 ok, cool, i will check out some of your files! :D

author
RedFlash made it! (author)2009-09-04

Your code is loopy, when I executed it, my computer started going crazy. Scroll bars going loopy, when I tried to delete the file, it kept on opening it. I had to reboot my computer to stop it. Not good :(

author
Mirror Mage made it! (author)2009-08-09

both this one and the one by smith2martin just doesn't work

author
shoyru_master_11 made it! (author)2009-08-01

BAD VBS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Does not work

author
geeklord made it! (author)2008-09-24

Alright then. How do you get this "unkillable" box to go away without shutting down ur computer? Task manager isn't finding it either.....

author
munchman made it! (author)munchman2008-09-30

A VBScript file is not an executable. The files are handled by the windows scripting host.. Kill wscript.exe

author
geeklord made it! (author)geeklord2008-10-01

doesn't even pull that up

author
munchman made it! (author)munchman2008-10-02

Make sure you are in the processes tab. What version of windows do you have? That's what it's called in XP (at least SP2)

author
munchman made it! (author)munchman2008-10-02

Or, see if you can find cscript.exe in Task Manager. It is possible that it handles vbscript on your computer.

author
geeklord made it! (author)geeklord2009-07-13

thx, got it now

author
Master Of Hacks made it! (author)Master Of Hacks2009-07-13

Go to task manager, "Processes" tab, then find wscript.exe and kill it. This goes for all VBS running scripts.

author
will421 made it! (author)2009-05-28

How do you open notepad and make it maximized?

author
El Mano made it! (author)2009-02-03

An interesting -ible for a programmer who knows the basic structure and operation of VBS. As for me, all I can do is get my computer to display a basic Message Box that I isolated from a VBS password-protection program. Perhaps you could write an instructable that will explain how VBS works: how to set and use variables, program organization, and the like. Then you could put a list of commands like "Msgbox("hi")" at the end, so instructees could learn how to construct a basic program and then have a quick reference of functions. Show people how to use a command and why it works, instead of simply saying "This creates a message box! Have fun!"

author
J-Manoo7 made it! (author)J-Manoo72009-04-21

agreed

author
smart punker24 made it! (author)2009-01-30

I don't get it explane it more simply

author
nativman made it! (author)2008-11-19

hello i will be posting later a vbs that will act as run.reply me if you want.

author
nativman made it! (author)nativman2008-11-23

dim x
dim oShell
Set oShell=WScript.CreateObject("WScript.Shell")
x = Inputbox("Run")
oShell.Run x

Note: this only work for the ff. script host
1.WScript Scripting host
2.CScript Scripting host
unless you own a vbs editor.

About This Instructable

148,406views

34favorites

License:

Bio: I am
More by mdkelsh:Setup a VPN for your iPhone/iPad or ComputerCreating a Home Media Streaming Center with UbuntuHot Fudge Pudding Cake
Add instructable to: