Running Script Files from the Command Prompt


The Short Story

To run a VbScript from the command line:

Change the script file extension to .cmd. E.g. MyScript.vbs => MySript.cmd

Start the file with some magic:

' cscript.exe //E:VBScript //NoLogo "%~f0" %*

'Put VbScript starting here.


MyScript.cmd is now a cmd file that use VbScript.

The Long Story

Dos/Windows uses file extensions to specify what application should interpret a file, when the file is double clicked or run as a command.

So .doc files are opened with Word; .txt files are opened with Notepad. This association is kept in the registry. VBScript (.vbs) typically cannot be run from the command line or by double clicking.  Savvy users have removed the association between vbs files and the VbScript engine, since vbs files were a common way to email viruses (virii).


While it is possible to run a script file using a command:

          ' cscript.exe //E:VBScript //NoLogo MyScript.vbs


This is not easy for most users.


Finally, VBScripts may still be run in the console or windows mode, typically a script has been designed to run in just one.


Other scripting engines have the same limitations. These include alternate command shells, and Beyond Compare.

Specifying the Script Processor From Within a Script


This 'trick' is stolen from Unix, where any text file may specify its 'scripting engine' in  the first line of the script. In Unix script files you may seen a first line like:



#! /usr/local/bin/perl


This line tells Unix what script processor should be used to interpret this file. This allows Bourne shell users to execute csh (c shell) command files without any special commands. It is also used to introduce perl scripts that may be run from the command line.


The Trick

Something like this Unix trick can be done in Windows. The Windows trick is to construct this line so that it interpreted by cmd, and then ignored by the scripting engine. Otherwise, the scripting engine, would loop endlessly re-interpreting t his line.


For example # is significant to cmd, but is a comment in perl or Beyond Compare.

A first line:

#Perl "%~f0" %*

is interpreted by cmd as run a file called ‘#perl’. A file called ‘#perl.cmd’’ is then used to load perl using the script file as input.




When perl begins to read the script file, it interprets this line as a comment, and continues to interpret the rest of the script.


Running VbScript Files Without a File Association

The trick with vbs files goes one better. Since ‘ is ignored by cmd AND introduces a comment to cscript, we can use a first line like:

' cscript.exe //E:VBScript //NoLogo "%~f0" %*

at the start of a .cmd file. The rest of the file is interpreted as VbScript.


1. The first line execute cscript.exe using this file has the script

The leading ' is ignored by cmd


2. When cscript opens the script file, it ignores the first line since it is a VbScript comment.



So what does this line do?


' cscript.exe //E:VBScript //NoLogo "%~f0" %*


cscript.exe –The file to start from CMD. This must be a full path or cscript must be on your PATH. Since script is usually at "C:\WINDOWS\system32\cscript.exe", this is not a problem.


//E -- specifies the scripting engine. You could specify the Windows Host, if you script should run w/o a console window.


//NoLogo -- Suppresses the banner at startup. This includes a bunch of junk that is displayed by default:

Microsoft (R) Windows Script Host Version 5.7

Copyright (C) Microsoft Corporation. All rights reserved.  


"%~f0" –- Cmd expands the first argument to cmd %0, the name of the script file, to its full path. Since %~f strips quotes, they are added here.


%* -- Cmd expands to the rest of the arguments 1…N from the original (initiating) command line.




Beyond Compare

The trick works for other scripting engines. Beyond Compare uses # to introduce a comment. In this case the header is:

#bc2 @"%~f0" %* /readonly


Since cmd does not ignore the #, this also requires an additional file on the PATH



@start /w bc2 %*


Here CMD executes the 1st line, starting a file called "#bc2.cmd".

#bc2.cmd then starts beyond compare (bc2.exe), which must be on your path or you may specify its path here.


Updated 10/29/2008 1:43:01 PM