Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Create a VBScript File in Windows

Write a small Visual Basic Script, save it as a .vbs file and run it.

Windows Hosting2 min read5 steps6 screenshots

What VBScript is

VBScript (Visual Basic Script) is a light scripting language built into Windows. It is good for small jobs such as pop-up messages, file handling and system tasks. Scripts are plain text files.

Your first script

  1. Open Notepad from the Start menu.
    Opening Notepad
  2. Type this line:
MsgBox "Hello, world!"
A VBScript in Notepad

MsgBox shows a message box with your text.

  1. Click File, then Save As. Choose All Files as the type and name the file myscript.vbs.
    Saving the file with the .vbs extension

Run it

Double-click the file. The message box appears.

The Hello World message box

Or use Command Prompt:

  1. Search for cmd in the Start menu and open it.
    Opening Command Prompt
  2. Go to the folder of the script with cd and run:
cscript myscript.vbs
Running the script with cscript

Basic elements

Variables. Declare them with Dim. VBScript has no type names, every variable is a Variant:

Dim name
name = "Hostvento"

Value types you can store: text (String), whole numbers (Integer), decimals (Double), True or False (Boolean) and dates.

Operators

  • Arithmetic: +, -, *, /, ^
  • Comparison: =, <, >, <=, >=, <>
  • Logical: And, Or, Not

Control structures

  • If ... Then ... Else for choices
  • For ... Next and Do ... Loop for loops
  • Select Case for many choices

A practical example

This script asks for two numbers and shows their sum. CDbl turns the text from the input box into a number:

Dim num1, num2, result
num1 = CDbl(InputBox("Enter the first number:"))
num2 = CDbl(InputBox("Enter the second number:"))
result = num1 + num2
MsgBox "The result is: " & result

Good to know

  • Use On Error Resume Next to handle errors.
  • VBScript supports regular expressions.
  • Use the FileSystemObject to work with files and folders.
  • WScript.Shell lets a script run other programs.
  • Modern Windows versions still run VBScript, but Microsoft plans to remove it in a future release. For new work, PowerShell is a better choice.

Quick recap

  • Save your code as a .vbs file.
  • Double-click it, or run cscript file.vbs.
  • Use Dim, If, loops and MsgBox as the base.