Windows Scripting Basics

Microsoft has built an incredibly powerful scripting language that can control almost every aspect of the Windows operating system. What I will be discussing here are only the basics that apply mostly to windows XP. Windows scripting has been around since Windows 98 but as the operating systems have progressed the scripting language has as well.

All the tools that we need to created windows scripts are included in windows and the is simply notepad. You can get other editors to use such as Script Editor, Scintilla and SciTE, and many more. But all that you need is Notepad and you are good to go. When you create a scripting file you will create any name you want and you can give it the extension of .VBS (Visual Basic Script).

Lets write out first script, the famous Hello World script, by opening up notepad and simply typing:

wscript.echo “Hello World”

Now simply double click on the file that you have created and you will see a pop up box that will display the text “Hello World” give it a try. This script can also be executed by a command prompt by typing “cscript hello.vbs” from the command prompt.

All good programmers comment their programs so that years from now they can look back at a program they wrote and tell what it is doing based on the comments. A comment is simply text in the program that is only seen by the programmer and never by the person running the program. You can type anything you want in a comment and it will not effect the program in any way. Professional programmers comment on every aspect of their program so that others who read the code can understand what it is doing. To add a comment in a VBS file simply type REM or ‘ at the beginning of a line and every thing on that line will be a remark and will have no effect on the program. Lets look at our Hello.vbs program and lets add a couple of remarks in the program so that we know what is going on.

‘ hello.vbs

‘ This is just your typical Hello World program

wscript.echo “Hello World”

If you happen to type something wrong windows will give you an error telling you that you have done something wrong and will even give you the line that the error is on. Lets mess up our program and see what kind of error we receive.

‘ hello.vbs

‘ This is just your typical Hello World program

wscript.echo “Hello World”

wscript.ech “I messed up this line”

Now lets run the program and see what kind of error we get. The first line is executed just fine but we receive a error on the second line:

Script: c:usersmikedesktophello.vbs

Line: 5

Char: 1

Error: Object doesn’t support this property or method: ‘wscript.ech’

Code: 800A01B6

Source: Microsoft VBScript runtime error

So as you can see from this error that we have a problem on line 5 character 1 of our code. The error Object doesn’t support this property or method: ‘wscript.ech’ usually means that you have a misspelling somewhere. The Code is not really that important to know just the location of the problem and what the “Error” actually says.

As with any programming language VBScript uses variables to hold values and strings. To assign a value to a variable simply use the variable name you would like and then the = sign. One examble would be ‘mike = 24’ now the variable mike has a value of 24, now lets add this to our Hello World program and show that the variable mike has a value of 24.

‘ hello.vbs

‘ This is your typical Hello World program

wscript.echo “Hello World”

mike = 24

jake = 30

diff = jake – mike

wscript.echo “Mike is “, mike

wscript.echo “Jake is “, jake

wscript.echo “The difference between their age is “, diff

As you can see here we assigned the value 24 to the variable mike and we assigned the value of 30 to the variable jake. We were then able to perform and mathmatical operation on the two values to get the difference between them. Then by using the wscript.echo command we were able to display the variables. Lets continue on with variables and lets store a string which is stored as text and we will get input from our user.

‘ hello.vbs

‘ This is your typical Hello World program

name = InputBox(“What is your name?”)

age = InputBox(“How old are you?”)

wscript.echo “Hello “, name

wscript.echo age, ” is a good age”

Do get the input box to pop up we will use the InputBox command and use the = sign to assign what the user types in to a variable and then we are able to print the results on the screen. Now lets go a little further and lets have the program make a decision about what to do based on what the user types. To make this program make a decision we will be using the IF… ELSE IF… ELSE… statements.

‘ hello.vbs

‘ This is your typical Hello World program

name = InputBox(“What is your name?”)

age = InputBox(“How old are you?”)

wscript.echo “Hello “, name

If age 17 AND age 17 AND age


Leave a Comment