Microsoft KB Archive/827075

= How to create Windows scripts by using Visual Studio 2005 or Visual Studio .NET =

Article ID: 827075

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Simple JScript File (JScript1.js)
 * Create a Simple VBScript File (VBScript1.vbs)
 * Create an JScript File (JScript2.js) That Contains a Function
 * Create a Windows Script Host File (WindowsScript1.wsf)
 * Include the JScript2.js File in Your Windows Script Host File
 * Run Your Scripts
 * REFERENCES



SUMMARY
This step-by-step article describes how to create Microsoft Windows scripts by using Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. You can run a Windows script by double-clicking the script file in Windows Explorer.

Microsoft Windows Script Host (WSH) is a language-independent scripting host for script engines that are compatible with Windows scripts. WSH creates an environment for hosting scripts. It runs text files that are stand-alone scripts. When you run a script, WSH provides objects and services for the script. You can use WSH to run scripts both on the Windows desktop and at a command prompt. WSH is built into Microsoft Windows 98 or later.

Typically, Windows scripts are written in either Microsoft JScript or Microsoft Visual Basic Scripting Edition (VBScript). The script engines for these programming languages are included with Windows 98 or later. A stand-alone script that is written in JScript has the .js file name extension. A stand-alone script that is written in VBScript has the .vbs file name extension. These file name extensions are registered with Windows.

You may also write Windows scripts in other languages, such as Perl, REXX, and Python. However, you must register the script engines for these programming languages with Windows. When you run a registered Windows script, Windows starts WSH. WSH activates the associated script engine to interpret and to run the script.

You can use Visual Studio 2005 or Visual Studio .NET to develop scripts in JScript and VBScript. You can also use Visual Studio 2005 or Visual Studio .NET to develop WSH files. A WSH file has the .wsf file name extension. WSH files may include other script files as parts of the script. Therefore, multiple WSH files may reference libraries of useful functions that are created and stored in one location. Additionally, you can combine JScript and VBScript in a single WSH file.

You can also use Visual Studio 2005 or Visual Studio .NET to debug scripts. However, when you install Visual Studio 2005 or Visual Studio .NET, you automatically install the Visual Studio 2005 or Visual Studio .NET debugger as the default debugger for scripts. Therefore, the Visual Studio 2005 or Visual Studio .NET debugger replaces the Windows Script Debugger in applications such as Internet Explorer.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000, Microsoft Windows XP, or Microsoft Windows Server 2003
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topic:
 * Microsoft Windows Script Technologies

back to the top

Create a Simple JScript File (JScript1.js)
 Start Windows Explorer. In a convenient location, create a folder that is named MyFolder. Start Visual Studio 2005 or Visual Studio .NET. On the File menu, point to New, and then click File. The New File dialog box appears. Under Categories, click Script. Under Templates, click JScript File. Click Open. By default, the JScript1 file is created.</li>  In the JScript1 file, locate the following code: // JScript source code </li>  Paste the following code after the code that you located in step 8: // Display a message. WScript.Echo(&quot;Simple JScript File&quot;); </li> On the File menu, click Save JScript1 As. The Save File As dialog box appears.</li> In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2.</li> Click Save. The JScript1.js file is created in MyFolder.</li> On the File menu, click Close.</li></ol>

back to the top

Create a Simple VBScript File (VBScript1.vbs)
<ol> In Visual Studio 2005 or in Visual Studio .NET, point to New on the File menu, and then click File. The New File dialog box appears.</li> Under Categories, click Script.</li> Under Templates, click VBScript File.</li> Click Open. By default, the VBScript1 file is created.</li>  In the VBScript1 file, locate the following code: ' VBScript source code </li>  Paste the following code after the code that you located in step 5: ' Display a message. WScript.Echo &quot;Simple VBScript File&quot; </li> On the File menu, click Save VBScript1 As. The Save File As dialog box appears.</li> In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the &quot;Create a Simple JScript File (JScript1.js)&quot; section of this article.</li> Click Save. The VBScript1.vbs file is created in MyFolder.</li> On the File menu, click Close.</li></ol>

back to the top

Create a JScript File (JScript2.js) That Contains a Function
<ol> <li>In Visual Studio 2005 or in Visual Studio .NET, point to New on the File menu, and then click File. The New File dialog box appears.</li> <li>Under Categories, click Script.</li> <li>Under Templates, click JScript File.</li> <li>Click Open. By default, the JScript2 file is created.</li> <li> In the JScript2 file, locate the following code: // JScript source code </li> <li> Paste the following code after the code that you located in step 5: // MyFunction is a function that returns &quot;MyFunction&quot;. function MyFunction{ return &quot;MyFunction&quot;; } </li> <li>On the File menu, click Save JScript2 As. The Save File As dialog box appears.</li> <li>In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the &quot;Create a Simple JScript File (JScript1.js)&quot; section of this article.</li> <li>Click Save. The JScript2.js file is created in MyFolder.</li> <li>On the File menu, click Close.</li></ol>

back to the top

Create a Windows Script Host File (WindowsScript1.wsf)

 * 1) In Visual Studio 2005 or in Visual Studio .NET, point to New on the File menu, and then click File. The New File dialog box appears.
 * 2) Under Categories, click Script.
 * 3) Under Templates, click Windows Script Host.
 * 4) Click Open. By default, the WindowsScript1 file is created.

back to the top

Include the JScript2.js File in Your Windows Script Host File
<ol> <li> On the View menu in Visual Studio 2005 or in Visual Studio .NET, click HTML Source, and then locate the following code in the WindowsScript1 file: <script language=&quot;JScript&quot;> In Visual Studio .NET 2002, locate the following code in the WindowsScript1 file: <script language=&quot;JScript&quot;> </li> <li> Replace the code that you located in step 1 with the following code: <script language=&quot;JScript&quot; src=&quot;JScript2.js&quot;> </li> <li> In the WindowsScript1 file, locate the following code: WScript.Echo &quot;VBScript&quot; </li> <li> Paste the following code after the code that you located in step 3: ' Retrieve the return value of the MyFunction function _ ' into the retVal variable. retVal = MyFunction ' Display the retrieved return value. WScript.Echo retVal </li> <li>On the File menu, click Save WindowsScript1 As. The Save File As dialog box appears.</li> <li>In the Save File As dialog box, locate and then click the MyFolder folder that you created in step 2 of the &quot;Create a Simple JScript File (JScript1.js)&quot; section of this article.</li> <li>Click Save. The WindowsScript1.wsf file is created in MyFolder.</li> <li>On the File menu, click Close.</li></ol>

back to the top

Run Your Scripts
<ol> <li>Switch to Windows Explorer.</li> <li>Locate and then click the MyFolder folder that you created in step 2 of the &quot;Create a Simple JScript File (JScript1.js)&quot; section of this article.</li> <li>Double-click the JScript1.js file. A Windows Script Host message box appears that contains the following text:

Simple JScript File

</li> <li>Click OK to close this message box.</li> <li>Double-click the VBScript1.vbs file. A Windows Script Host message box appears that contains the following text:

Simple VBScript File

</li> <li>Click OK to close this message box.</li> <li>Double-click the WindowsScript1.wsf file. A Windows Script Host message box appears that contains the following text:

JScript

</li> <li>Click OK to close this message box. A Windows Script Host message box appears that contains the following text:

VBScript

</li> <li>Click OK to close this message box. A Windows Script Host message box appears that contains the following text:

MyFunction

</li> <li>Click OK to close this message box.</li></ol>

back to the top

<div class="references_section">