# VBScript Programming

VBScript, short for Visual Basic Scripting Edition, is a Microsoft-supplied scripting language similar to Visual Basic and Visual Basic for Applications. It can be used via Windows Script Host (WSH) as a Windows scripting language. Furthermore, it can be used as a client-side scripting language in web pages instead of JavaScript, but such pages will only work with Internet Explorer. Moreover, it can be used for server-side web scripting as part of Active Server Pages (ASP), and in other environments. This book focuses on the language use via WSH.
Scripts to be used with WSH are usually plain text files with .vbs extension. By default, Windows associates the extension with an interpreter of the language, and therefore, entering the script name on the Windows batch command line leads to script execution, albeit without waiting for script completion. Furthermore, scripts can be run from the desktop or the Windows file explorer, usually by double clicking.
VBScript can access objects available via Microsoft COM interface, including those of Microsoft Office. Thus, Microsoft Office can be scripted not only from Visual Basic for Applications (VBA) embedded in office files such as spreadsheets but also from VBScript scripts, in a syntanctically similar yet somewhat different manner.
Two commands are available to let WSH execute a script: wscript and cscript. wscript is for interaction via simple popup windows whereas cscript is for command line interaction via text console. By default, .vbs extension is associated with wscript.
VBScript is mostly case-insensitive as for keywords and identifiers. Thus, "End If" is equivalent to "end if", and "FSO.CreateTextFile( ..." is equivalent to "fso.createtextfile( ...".

## Data types

Unlike in VBA, variables cannot be dimensioned to be restricted to values of a particular data type, such as String. Rather, they are all of data type Variant.
However, there is still an underlying data type of each variable, depending on what you assign to the variable. You can find out about the current underlying data type of a variable content using TypeName function (returns a string) and VarType function (returns an integer).
Links:
* VBScript Data Types, Microsoft Docs
* TypeName, ss64.com
* VarType Function, docs.microsoft.com
* VBScript built-in functions, ss64.com

## Literals

Examples of literals of various data types:
Examples that are not of literals but serve the same purpose:

## Dictionary

VBScript supports dictionaries AKA associative arrays via Scripting.Dictionary object. The key can be of any type except for arrays; thus, strings, integers, floating points, dates, etc. are supported.
Examples:
Links:
* Dictionary Object, docs.microsoft.com

## Input and output

For input and output for user interaction, VBScript in Windows Scripting Host offers a simple popup message box and a simple popup input box, which work for both script invocations via wscript (the default) and cscript. Text console input and output is not supported in scripts run via wscript; running via cscript is required.
Examples of input via WScript object, primarily via text console:
Examples of input and output via popup windows, both for wscript and cscript:
For reading from a text file and writing to a text file, see #File operations.
Example of getting command-line arguments:
Example of getting an environment variable:
Links:
* .Echo, ss64.com
* StdOut.Write, ss64.com
* .StdOut.ReadLine , ss64.com
* MsgBox, ss64.com
* InputBox, ss64.com
* How-to: A list of useful WScript properties, ss64.com
* How-to: VBScript command line arguments, ss64.com
* .ExpandEnvironmentStrings, ss64.com

## File operations

Unlike in VBA, file operations are not part of the language built-in core but rather are accessible from FileSystemObject.
Reading from a text file:
Writing to a text file:
Getting current directory:
Reading bytes from a binary file using ADODB.Stream:
Writing bytes to a binary file using ADODB.Stream:
Above, the trick is to use iso-8859-1 together with ChrW. A caveat is that iso-8859-1 does not define control codes so their mapping from Unicode to iso-8859-1 is formally undefined, but an implementation-defined mapping was chosen in ADODB.Stream and so it works anyway. The above cannot be reliably achieved using FileSystemObject by writing Chr(num) to a text file since that is only going to work in some locales but not others.
Links:
* FileSystemObject, ss64.com
* FileSystemObject, Microsoft Docs
* Stream Object (ADO), docs.microsoft.com
* Binary Files and the File System Object Do Not Mix, blogs.msdn.microsoft.com
* Is ISO-8859-1 a Unicode charset?, stackoverflow.com
* ISO/IEC_8859-1, wikipedia.rog

## Byte array

Byte arrays are to be contrasted with variant arrays of bytes. In VBScript, all declared arrays are variant arrays and it has no direct and simple method of creating a byte array. Still, a byte array can be created, even if in a more wordy manner and in dependence on ADODB.Stream.
Populating a byte array and then read-iterating it:
Above, populating a byte array uses writing Unicode code points in the range of 0-255 in iso-8859-1 encoding into a text stream, copying the text stream into a binary stream, and then reading the bytes from the binary stream. Read-iterating the byte array is brief and uses the trick of combined MidB and AscB rather than array index access.

## Operators and math

Arithmetic operators include +, -, *, / (true division), \ (integer division), ^ (exponentiation) and mod (modulo). Integer division truncates toward zero, like in the C language: -3 \ 2 = -1. Integer division converts the operands to integral types (rounding them) before performing the division: 3.6 \ 2 = 2 (Long). Modulo is a remainder after the truncate-toward-zero integer division, and therefore, -3 mod 2 = -1 like C's -3 % 2 rather than like Python's -3 % 2 == 1.
Comparison operators include =, <>, <, >, <=, >= and Is.
Bitwise operators include And, Or, Xor, Not, Imp and Eqv. These operate on integer types (Byte, Integer, Long) as bitwise operators. When operating on negative integers, they act in keeping with the two's complement internal representation of signed integers. There are no bitwise shift and bitwise rotation operators, but they can be emulated.
Boolean logical operators are the same as bitwise operators. When operating on booleans including boolean expressions, the bitwise operators double as boolean logical operators with no short-circuit evaluation by True being -1. Thus, True and 14 yields 14 since -1 as Integer (16-bit) is 0xFFFF.
Math functions include Abs, Atn, Cos, Exp, Fix, Int, Log, Randomize, Rnd, Round, Sgn, Sin, Sqr and Tan.
String concatenation operators are & (recommended) and + (not recommended).
In another wikibook, see Active Server Pages/Appendix A: Language Reference.
Links:
* Operators (VBScript), docs.microsoft.com
* VBScript Features, docs.microsoft.com
* How-to: VB Script Operators, ss64.com
* VBScript built-in functions, ss64.com

## Excel

Excel can be scripted using VBScript by accessing Excel.Application object. Unlike VBA, VBScript does not support named argument passing to methods, only positional argument passing. Furthermore, functions that appear global from VBA in Excel need to be called as methods of the Excel object in VBScript. Moreover, Excel's specific constants such as xlAnd are not available; you need to define the constant yourself or use a number instead.
An example:
Links:
* Microsoft Excel Constants, Microsoft Docs

## Constants

There are multiple built-in constants, starting with vb. For instance, vbOKCancel is used in conjunction with MsgBox.
Applications-specific constants such as Excel's xlAnd are not available.
Links:
* VBScript Built-In Constants, ss64.com
* Microsoft Excel Constants, Microsoft Docs
* Microsoft Outlook Constants, Microsoft Docs
* Word Enumerated Constants, Microsoft Docs

## Clipboard

VBScript does not support VBA's MSForms.DataObject to access clipboard.
There are workarounds:
* For writing to the clipboard, you can run clip.exe available in Windows 7.
* For reading from the clipboard, you can access Internet Explorer via COM and let it read from the clipboard.
Links:
* VBScript, Text to Clipboard to Paste in Any Field, stackoverflow.com

## External processes

You can run external processes using Run method of WScript.Shell:
You can also do so using Exec method of WScript.Shell:
Keywords: external commands, running programs.
In another Wikibook: Excel VBA#Command Output.
Links:
* Running Programs in Microsoft Windows 2000 Scripting Guide, TechNet Archive, Microsoft Docs
* .Run, ss64.com
* The wscript.Shell + Shell.Application objects, ss64.com

## Regular expressions

You can use regular expressions using RexExp object:
Alternatively, you could create the regex object via Set RegExp = CreateObject("VBScript.RegExp"), but, in VBScript, that is unnecessary.
Links:
* Microsoft Beefs Up VBScript with Regular Expressions , docs.microsoft.com
* VBScript’s Regular Expression Support, regular-expressions.info

## Unicode

There is the following support for 16-bit Unicode in VBScript with WSH on platforms that support Unicode:
* Script source code can be stored in UTF-16 LE (litte endian), and then, string literals can be in Unicode. Variable names and procedure names cannot be in Unicode. UTF-16 BE (big endian) and UTF-8 are not supported. A script in UTF-8 with BOM will not run. A script in UTF-8 without BOM may run but string literals with characters beyond 127 will be garbled when output via MsgBox and other interfaces.
* UCase and LCase work with Unicode, so does MsgBox.
* Unicode characters can be entered via ChrW function by passing it a code point number. ChrW(127) = Chr(127) while ChrW(128) <> Chr(128).
* Unicode code point number of a character can be obtained via AscW.
* FileSystemObject methods OpenTextFile and CreateTextFile support reading and writing UTF-16 LE Unicode when instructed to do so via optional parameters.
* ADODB.Stream object supports writing Unicode to a file and reading from a file in UTF-8 by setting Stream.Charset to "utf-8", and in other encodings such as "ascii" or "iso-8859-1". You can obtain a list of encodings from cmd.exe command line via "reg query HKEY_CLASSES_ROOT\MIME\Database\Charset" or from PowerShell via "dir Registry::HKEY_CLASSES_ROOT\MIME\Database\Charset".
Links:
* Stream Object (ADO), docs.microsoft.com
* Convert UTF-8 file to UTF-16 BE file in Vbscript, stackoverflow.com
* ActiveX Data Objects, wikipedia.org
* FileSystemObject, ss64.com
* FileSystemObject, Microsoft Docs

## Reserved keywords

VBScript has the expected reserved keywords such as Dim or While; these cannot be used as variable names. However, it has also some reserved keywords that come from VBA and are not used by VBScript syntax, such as Byte and Long.

## Versions

VBScript versions include 5.1 (Win 2000), 5.6 (XP), 5.7 (Vista) and 5.8 (Win 7, Win 10).
The table of added features per version in the linked Microsoft documentation shows no added features after year 2000.
Script host version is to be distinguished from the script engine version.
To find out about the engine version and the Windows Script Host version:
Running cscript without //nologo switch outputs script host version.
Links:
* VBScript Version Information, Microsoft Docs
* Windows Script Host#Version history, wikipedia.org

## Comparison to VBA

Features missing from VBScript while present in VBA:
* Named argument passing to functions and methods
* Application-specific named constants such as Excel's xlAnd are not available; you have to set them on your own or pass numbers instead
* Built-in file I/O; VBScript can use FileSystemObject
* Creating custom collections via new Collection
* Dimensioning variables with particular data types
* Etc.
Links:
* Visual Basic for Applications Features Not In VBScript, Microsoft Docs
* Does VBScript allow named arguments in function calls?, stackoverflow.com

## Limitations

Limitations:
* Limitations described in #Comparison to VBA apply. In particular, there is no way to create custom Collections and thus no support for trivially expandable lists known from many programming languages; workarounds include redimensioning dynamic arrays as required or storing indices as numerical keys in a dictionary.
* In general, compared e.g. to .NET-based PowerShell or to Python, very few facilities available as libraries; by contrast, the two mentioned scripting technologies sport very many libraries to support a variety of tasks.
* No interactive shell to execute VBS commands one at a time, unlike e.g. .NET-based PowerShell or Python.
* No reading from and writing to a console for scripts run via wscript, only for scripts run via cscript.
* Limited support for array operations, e.g. no sorting functions. Users resort to writing their own.
* No direct way of including other .vbs scripts as libraries; an indirect way is via WSF XML files.
* No bitwise shift left and shift right operators, but can be easily implemented.
* No general GUI programming from .vbs scripts; an alternative is to embed VBS in a HTML as HTML Application (HTA). Simple message boxes and input boxes are supported directly without HTA.
* No arbitrary precision integer arithmetic.
* No set type (mathematical set).
* And more.

## Popularity and adoption

VBScript in Windows Scripting Host used to be popular with Windows administrators. As of 2022, its use has since long dwindled and has been vastly surpassed by that of PowerShell. VBScript engine saw no major update since 2001 and Microsoft has positioned PowerShell as the preferred scripting technology for Windows administration. VBScript in Classic ASP has since long been surpassed by VB.NET and C# in ASP.NET. Unlike VBScript, VBA continues to see widespread use.
Links:
* VBScript, PowerShell, Visual Basic for Applications, COBOL, trends.google.com
* VBScript,PowerShell,VBA,COBOL in Google Ngram Viewer, google.com
* vbscript, vba, powershell in Stack Overflow Trends, stackoverflow.com
* TIOBE Index, tiobe.com

## COM components

COM components often used with VBScript in WSH:
* WScript.Shell
* WScript.Network
* Scripting.FileSystemObject
* Scripting.Dictionary
* Shell.Application
* Excel.Application
* Word.Application
* Outlook.Application
* InternetExplorer.Application
Links:
* Windows Script Host Object Model, Microsoft Docs

## Related Wikibooks

* Active Server Pages
* Active Server Pages/Appendix A: Language Reference -- a single-page overview
* Visual Basic
* Excel VBA
* Visual Basic for Applications

## External links

* VBScript Language Reference, Microsoft Docs
* VBScript Language Reference, identified as vs.85, docs.microsoft.com
* VBScript Features, docs.microsoft.com -- an index of keywords grouped by category
* WSH Primer, TechNet Archive, Microsoft Docs
* Fabulous Adventures In Coding, a blog, microsoft.com
* VBScript Commands, ss64.com
* VBScript How-to guides and examples, ss64.com
* VBScript, wikipedia.org
* Windows Script Host, wikipedia.org
* Category:VBScript, rosettacode.org
* JavaScript and VBScript Code Comparison, harding.edu - a single-page cheat sheet