VBScript – Getting Started

Getting Started with VBScript

This section is for beginners who are copying and pasting example scripts.

My sample scripts will work on Windows 7, Server 2003/8 and XP.  Avoid other clients like Windows 98 unless you get theWSH updates

 ♦

Instructions to Create Your VBScript

  1. Navigate to the page where you want to copy the example ‘VBScript.
  2. Copy all the lines inside the blue section of my sample script (Ctrl + c).
  3. Open notepad.exe, now paste the script text (Ctrl +V).
  4. In notepad, go to the File Menu, Save As script.vbs  – The .vbs extension is vital, do not let it save as a .txt file.
  5. To run the script simply double click your script.vbs (or what ever you called the file).

Example 1 –  A Simple Script to Display Your ComputerName

‘ VBScript to Display your ComputerName
‘ Created by Guy Thomas Jan 2003

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "You Computer Name = " & WshNetwork.ComputerName

If you are ready to move on then try: – Creating Computers

Example 2 – A Script to Display Your VBScript Version Number

  (If this script fails then you need a Windows or XP machine.)

‘ VBScript
‘ Created by Guy Thomas Jan 2003
‘ Simple script to check VBScript VERSION

WScript.Echo "VBScript Version: " & ScriptEngineMajorVersion _
& "." & ScriptEngineMinorVersion
WScript.Quit

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool

Traps When Creating VBScripts

  • You missed the first ‘ apostrophe – make sure you start at the beginning of the line.
  • You execute the script a second time and get a  Code 80071329  – Object Already Exists, WSH message box.  The reason is that each object must have a unique name.  So if you created – say a user, you cannot run the script again without either modifying a line in the script, or deleting the first object.

Summary of VBScript Getting Started

The first section of the VBScript, sometimes referred to as the header, states the purpose of the script and declares variables in Dim statements.

I like to start the script proper with Option Explicit because it forces me to declare variables.  My reasoning is this, Option Explicit alerts me to errors, it halts the script if I miss spell a variable, or use the singular colItem whereas I declared the plural colItems in the Dim statement.

When we move into the middle section of the script, WMI prepares the ground by connecting to the root\cimv2.  In the example below, once objWMIService gets a handle on the namespace, we select DiskDrive from Win32_’s many objects.  Moving into the final section, VBScript loops through all the properties that we are interested in, and finally Echoes the results to screen.

If you like this page then please share it with your friends

 


See more VBScript examples:

VBScript create users   • VBScript create contact  • Create contact Exchange  • VBS PwdLastSet

VBScript create computer   • PowerShell create computer from spreadsheet  • Free Import Users Tool

VBScript change password  • VBScript to create group  • SolarWinds Free WMI Monitor