The Basics Of Visual Basic Script

Ohav
3 min readOct 31, 2020

--

In the last couple of days I’ve found my self searching the web, looking for good tutorials and documentations about the not very famous VBScript. It wasn’t the easiest journey I have ever had, and because of that I’ll do my best to help future learners.

In this short article, I will present:

  • What is VBScript in the world of programming in 2020
  • How to start coding in VBScript
  • How to run and debug VBScript code
  • Snippets of basic programming techniques in VBScript

Visual Basic Script in 2020

VBScript is a scripting language by Microsoft used mainly for IIS (Microsoft Web Server), Internet Explorer and WSH (Windows Script Host). In this article we’ll use and focus on WSH when referring to VBScript.

Why WSH exists? It’s purpose was to be an automation technology that provides scripting abilities comparable to batch files, but with a wider range of supported features. In 2020, the reality is that most companies and developers will probably prefer to use Powershell or Python instead of VBScript.

However, one advantage that VBScript wins the others is that it comes installed by default on every Windows version since Windows 98!

Coding in VBScript

Looking for the perfect environment for coding this scripting language, there aren’t many options. I tried both Visual Studio Community and VSCode and decided to go with VSCode. There is a 60k downloaded extension called VBScript which offers a nice syntax highlighting for the language, as well as some code snippets. It’s pretty far from the convenience VSCode can give for other languages like Python or JavaScript, but I am glad we have at least that.

Like every new programming language, I’ve opened a hello.vbs file, and put the famous “Hello World” piece of code:

Wscript.Echo "Hello World!"

Running and Debugging VBScript code

In Windows 10 cscript.exe is the command-line version of the Windows Script Host, and all you need to do in order to run .vbs files with it, is to pass the path to the .vbs file:

C:\Users\John>cscript hello.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
Hello World!

In order to debug, all you have to do is to specify the /x flag when using cscript. Make sure you have Visual Studio (Community is enough) installed, with the .NET desktop development workload, and the following individual components:

  • Just-In-Time debugger
  • TypeScript 3.9 SDK
  • JavaScript and TypeScript language support

Basic programming Techniques in VBScript

I hope the following snippets can help you dive in a little smoother to coding in VBScript.

Variable Declaration and Data Types

Dim varInteger, varString, varDoublevarInteger = 20
varString = "30"
varDouble = 40.1
' Display Data Type of a variable
Wscript.Echo varType(varInteger) & ": " & typeName(varInteger)
Wscript.Echo varType(varString) & ": " & typeName(varString)
Wscript.Echo varType(varDouble) & ": " & typeName(varDouble)
' Output:
' 2: Integer
' 8: String
' 5: Double

Loops and Conditions

For i = 0 To 10 Step 2
Wscript.Echo i
If i = 6 Then
Wscript.Echo "I am done, Bye!"
Exit For
End If
Next
' Output:
' 0
' 2
' 4
' 6
' I am done, Bye!

Functions

Function congratsSomeone(name)
' Specify a return value:
congratsSomeone = "Congratulations " & name & "!"
End Function
Wscript.Echo congratsSomeone("Bill")
Wscript.Echo congratsSomeone("Gates")
' Output:
' Congratulations Bill!
' Congratulations Gates!

Arrays

Dim arr(2) ' Actually with the size of 3 items.arr(0) = "John"
arr(1) = 15
arr(2) = True
' It's easy to concatenate Strings, Integers and even Booleans!
Wscript.Echo arr(0) & arr(1) & " Is " & arr(2)
' Output: John15 Is True

Thanks for reading, feel free to share in the comments your experience with VBScript!

--

--