ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

Write VBScript

Updated on August 6, 2016

Learn How to Write VBScript

The power of VBScript, which interacts with the Windows script host, has been known by Administrators, developers, analysts and others since its inception in 1996. This scripting language, sometimes known as “vb scripting”, gives users the ability to search and monitor Active Directory, get user and computer information, move and delete files, test installed apps, monitor services, interact with databases, automate tasks and more.

For anyone working in IT or even a user who works with data in general, learning how to write VBScript is a handy skill to have on their resume. The ability to read and write VBScript is essential if moving into a job where prior administrators have written scripts that are used on a routine basis. Knowing how to write VBScript also provides a foundation to learn other scripting and programming languages.

Writing a few lines of VBScript, executing the script and understanding the results is probably one of the best ways to learn. Walk through the following steps to write a simple VBScript to gain an understanding of how simple the code is to write and how powerful it can be.

Write VBScript Code

write-vbscript | Source

Write the VBscript

Go to Start-->All Programs-->Accessories-->Notepad. Go to File-->Save As and save the file as “C:\Temp\readWriteDisplay.vbs”. Copy the following code into the lreadWriteDisplay.vbs file by selecting the code just to the left of “Option Explicit”, dragging the cursor just to the right of “WScript.Quit(), press “Ctrl” + “C”, select within the readWriteDisplay.vbs file, then press “Ctrl” + “V”. Select File-->Save within Notepad. Exit Notepad.

Option Explicit

'Define Variables

Dim fso

Dim objWriteFile

Dim objReadFile

Dim wshShl

Dim regKey

Dim RegRead

Dim strLine

'Set Objects

Set wshShl = WScript.CreateObject("WScript.Shell")

Set fso = CreateObject("Scripting.FileSystemObject")

Set objWriteFile = fso.OpenTextFile ("C:\temp\vbsResultsFile.txt",2,true)

Set objReadFile = fso.OpenTextFile("C:\temp\vbsResultsFile.txt", 1, False)

'Read registry key.

regKey = WshShl.RegRead ("HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProductName")

'Write regKey variable to text file.

objWriteFile.writeline regKey

'Read contents of text file.

strLine = objReadFile.readline

'Display results on screen

wscript.echo strLine

'Close text files.



'Cleanup Objects

Set wshShl = Nothing

Set fso = Nothing

Set objWriteFile = Nothing

Set objReadFile = Nothing


Write VBScript Results File

write-vbscript-results-file | Source

Write VBScript Messagebox

write-vbscript-messagebox | Source

Review VBScript Results

Navigate to “C:\Temp\” and double click on the readWriteDisplay.vbs file. A message will pop up stating the version of Windows being run on the computer that the script is executed from. Go to “C:\Temp\” and open up the vbsResultsFile.txt. Within this file, there will be a line which lists the current version of windows. The readWriteDisplay.vbs VBScript reads the Registry Key created in the 1st step, passes that on to a variable, writes that variable to the vbsResultsFile.txt file, reads the 1st line of that text file, sets that line to a variable, then displays that variable on screen in a message box. Look at the comments in the above code for further explanation. Comments are preceded by the “’” character.

‘This would be a comment. – This is a comment. Comments are not executed by VBScript

This would not be a comment .– VBScript would attempt to execute this line.


    0 of 8192 characters used
    Post Comment

    • madhuwillb profile image

      madhuwillb 5 years ago from Bangalore

      very nice hub