VBScript is a small, interpreted language derived from Visual Basic for Applications and designed for scripting: it has one data type (Variant), no declarations of types, no file statements and no direct access to the system. Everything outside the language (files, the registry, the network, a message box) comes from objects that the host provides. A script is a plain text file with the extension .vbs, normally ANSI or UTF-16 with a byte order mark.
Where it runs
- Windows Script Host:
cscript script.vbs(console) andwscript script.vbs(windowed); the host objectWScriptgivesEcho,Arguments,Quit,Sleep,StdIn,StdOutandCreateObject. Windows 11 version 24H2 made the VBScript feature an optional component that can be removed, which Microsoft has announced will be removed from later versions (from memory of Microsoft’s announcement; not checked on this machine). - Classic ASP pages (
<% … %>blocks in.aspfiles, with theResponse,Request,Server,SessionandApplicationobjects) and HTML Applications (.hta) and older Internet Explorer pages (<script language="VBScript">). - Other files:
.vbeis a script run through Microsoft’s script encoder (not readable text);.wsfis an XML Windows Script File that holds one or more<job>elements with<script language="VBScript">blocks (it is XML and is not coloured as VBScript).
The language
Option Explicit
Const ForReading = 1
Dim fso, file, line, counts, key
Set fso = CreateObject("Scripting.FileSystemObject")
Set counts = CreateObject("Scripting.Dictionary")
If WScript.Arguments.Count = 0 Then
WScript.Echo "usage: count.vbs file"
WScript.Quit 1
End If
On Error Resume Next
Set file = fso.OpenTextFile(WScript.Arguments(0), ForReading)
If Err.Number <> 0 Then
WScript.Echo "cannot open: " & Err.Description
WScript.Quit 2
End If
On Error GoTo 0
Do Until file.AtEndOfStream
line = Trim(file.ReadLine)
If Len(line) > 0 And Left(line, 1) <> "'" Then
counts(LCase(line)) = counts(LCase(line)) + 1
End If
Loop
file.Close
For Each key In counts.Keys
WScript.Echo key & ": " & counts(key)
Next
| Area | VBScript |
|---|---|
| types | every variable is a Variant; its subtype is one of Empty, Null, Integer, Long, Single, Double, Currency, Date, String, Object, Error, Boolean, Byte, Decimal or an array. There is no As clause and no type characters; the functions CInt, CLng, CDbl, CStr, CDate, CBool, CByte, CCur, CSng, TypeName and VarType convert and inspect |
| literals | decimal and floating numbers with E exponents, &H hexadecimal and &O octal integers, strings in double quotes with doubled quotes (one line only), date literals #1/1/2000# and the constants True, False, Empty, Null, Nothing and the vb… constants (vbCrLf, vbTab …) |
| declarations | Dim, ReDim [Preserve], Const, Public, Private; Option Explicit (the only Option) requires declarations; arrays are Dim a(9) (bounds 0 to 9) and Array(1, 2, 3) |
| statements | If … Then … ElseIf … Else … End If, Select Case, For … Next, For Each … Next, Do … Loop with While or Until, While … Wend, With, Call, Set, Exit, Randomize, Rem, Execute, ExecuteGlobal, Erase, Stop; the colon separates statements, _ continues a line |
| procedures | Sub and Function (the result is assigned to the name), Property Get, Let, Set in a class; parameters are ByRef unless ByVal; no Optional, no ParamArray, no named arguments, no Declare |
| classes | Class Name … End Class with Public and Private members, Class_Initialize and Class_Terminate, created with Set x = New Name; no inheritance and no Implements |
| errors | only On Error Resume Next and On Error GoTo 0 (no labels, no GoTo, no Resume); the Err object holds the last error (Number, Description, Source, Raise, Clear) |
| operators | ^ - * / \ Mod + &, = <> < > <= >= Is, Not And Or Xor Eqv Imp; no Like, no short-circuit operators |
| functions | strings (Left Right Mid Len Trim LTrim RTrim UCase LCase InStr InStrRev Replace Split Join Filter Space String StrComp StrReverse Asc Chr), numbers (Abs Sqr Sin Cos Tan Atn Exp Log Int Fix Round Rnd Sgn Hex Oct), dates (Now Date Time Timer DateAdd DateDiff DatePart DateSerial DateValue TimeSerial TimeValue Day Month Year Hour Minute Second Weekday WeekdayName MonthName), formatting (FormatNumber FormatCurrency FormatPercent FormatDateTime), arrays (Array UBound LBound), objects and interaction (CreateObject GetObject GetRef MsgBox InputBox LoadPicture RGB IsObject IsArray IsDate IsEmpty IsNull IsNumeric TypeName VarType), locale and encoding (GetLocale SetLocale Escape Unescape), Eval and ScriptEngine ScriptEngineMajorVersion ScriptEngineMinorVersion ScriptEngineBuildVersion. This is the table of functions in Microsoft’s archived VBScript reference (version 5.8); there is no Format function |
The objects scripts use most are WScript.Shell (Run, Exec, RegRead, Environment, SpecialFolders), Scripting.FileSystemObject (files, folders, text streams), Scripting.Dictionary, VBScript.RegExp (regular expressions), WScript.Network, Shell.Application, ADODB and MSXML2. They are COM objects, documented by their owners and not here.
Against VBA
VBScript is a subset with a few differences: no data type declarations, no Optional and ParamArray parameters, no GoTo, GoSub, On Error GoTo label or Resume, no file statements (Open, Print #), no Declare, no Const with a type, no Like, no Debug.Print, no events (WithEvents, RaiseEvent) and no Format; it adds Execute, ExecuteGlobal, Eval and the Class statement (a VBA class is a separate file). See VB6 and VBA.
Not described
Known, not yet covered
- Reference: only one page of Microsoft’s archived VBScript language reference (the table of functions,
learn.microsoft.com/previous-versions/…/3ca8tfek(v=vs.84)) and its introduction could be fetched; the other pages of the reference (statements, operators, objects, constants, the user’s guide) were not found at the addresses tried, and no public repository of them was found. The statements, operators and the absence ofLikeandGoToare therefore written from the VBA language reference, the real.vbsfiles on this Mac (22, all small) and knowledge of the language, and were not checked against the VBScript reference. - Reference: the version history of VBScript (1.0 to 5.8), the script encoder,
.wsfand.wshfiles, the objects of the hosts and the security policy of Windows Script Host are not described. - Viewer:
.vbe(encoded),.wsf,.htaand.aspare not read as VBScript.
See also
- VB6 and VBA – the language VBScript was cut from
- Versions and history