Choosing a Windows Scripting Language

A Quick Comparison of MS-DOS, VBScript and PowerScript

© Mark Alexander Bain

May 15, 2009
Comparing Windows Scripting Languages, Mark Alexander BainMark Alexander Bain
The Windows script developer can choose between MS-DOS (or CMD), VBScript and now PowerShell. There are pros and cons for each of them, so which is best?

When it comes to Windows scripting a programmer has a few languages that they can choose from. One has been around for as long as Windows itself, one is virtually brand new. Two will be very familiar to most programmers:

  • MS-DOS (or the Command prompt)
  • VBScript

But one may be new to many people:

  • PowerShell

It really comes down to what languages the programmer is used to and what it is that they’re wanting to do.

The Microsoft Command Prompt

The Microsoft command prompt comes in slightly different guises according the version of Windows installed on the user`s computer. For example it may be:

  • MS-DOS
  • command.exe
  • cmd.exe

Many simple tasks can be carried out with the command prompt such as:

  • creating directories
  • moving files
  • starting processes

It even allows simple "if" statements to be used, for example:

D = C:\Windows
if exist D echo D exists
if not exist D" echo D does not exist

And, most importantly, this can all be done by saving the commands to a script file (which requires a ".bat" file name extension). However, the scripting functionality is limited and the programmer will quickly need to turn to other scripting languages in order to carry out their work. Once such language is VBScript.

Microsoft VBScript

Microsoft VBScript (or Visual Basic Script) is (as it’s name suggests) derived from Microsoft Visual Basic. However, it must be emphasized that although VBScript is similar to Visual Basic it is not the same. This is a programming language in its own right. For example it enables if...then...else statements but instead of having specific built in functions, it uses system objects in order to carry out many operations, for example:

D = " C:\Windows"
set fs = CreateObject ("Scripting.FileSystemObject")
if fs.FileExists(D) then
msgbox D & " exists"
else
msgbox D & " does not exist"
end if

It's worth noting that VBScript is only a scripting language and is not a command line language. The code has to be saved into a file (normally with a ".vbs" file extension). The user can then run the file by double clicking or, or from the command line using the cscript command, for example:

cscript check_dir.vbs

The big advantage that VBScript has is that it can manipulate system objects, for instance it can:

  • create or modify a Microsoft Word or Excel document
  • manipulate the contents of a databases such as Microsoft Access or MySQL

Of course even VBScript is outdated now.

PowerShell

PowerShell is Microsoft Windows new scripting language and is based on the .NET framework. It combines the ability to carry out command line operations with the ability to create script files (which must always be saved with a ".ps1" file name extension). It also has access to all of the .NET objects installed on a computer.

A simple PowerShell script looks like:

$D = "C:\Windows"
if (test-path $D) {
write-output "$D exists"
} else {
write-output "$D does not exist"
}

The programmer can store this code to a file (for example c:\powershell\check_directory.ps1) and then run it from the command line:

powershell c:\powershell\check_directory

It should be noted that PowerShell (unlike VBScript and CMD) are not installed in Windows XP and Vista, although it is a part of Windows Server (and will be part of Windows 7). Any XP and Vista users will, therefore, need to download and install PowerShell.

Summary

The Microsoft Windows programmer has a number of options when it comes to scripting. They can:

  • use MS-DOS (or CMD) to create simple batch files
  • use VBScript for more complex programming
  • use PowerShell to make the most of the .NET framework

With one of these (or a mixture of them) the programmer has complete control of all of the objects on a Windows computer.

Further Reading

An Introduction to the Windows PowerShell


The copyright of the article Choosing a Windows Scripting Language in Command Line Programming is owned by Mark Alexander Bain. Permission to republish Choosing a Windows Scripting Language in print or online must be granted by the author in writing.


Comparing Windows Scripting Languages, Mark Alexander BainMark Alexander Bain
A PowerShell Script File, Mark Alexander Bain
The Output from a PowerShell Script, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Sep 29, 2009 7:31 AM
Guest :
Clear explanation - thanks, very useful intro
1 Comment: