Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (2/5/10)

Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (2/5/10)

  • Comments 1
  • Likes

Bookmark and Share

In this post:

How Can I Run My First VBScript Script?

Hey, Scripting Guy! Question

Hey, Scripting Guy!

I have followed the instructions from the VBScript first steps guide to the letter at least 20 times, but it still does not seem to work. I paste the code into Notepad and save it with the quotation marks. I then open a Command Prompt window, type CScript test.vbs, and press ENTER. These are the results: 

C:\Documents and settings\Denise>cscript test.vbs

Microsft  <R>   Windows script host version 5.7

Copyright  <C>  Microsoft corporation. All rights reserved.

 

Input error: Can not find script file  "C:\Documents and settings\Denise>test.vbs"-

 

Please help! Thank you.

-- HD

 

Hey, Scripting Guy! AnswerHello HD,

Navigating all those folders can be a bear. Perhaps a bear such as this one who was at TechEd 2009. I am going to assume you are on Windows XP, because of the space in Documents and Settings. One of the big improvements in Windows Vista is that we got rid of that space! It makes working from the command line much better.

Follow these steps to run your first VBScript script:

1. Open Notepad.

2. Type Wscript.Echo “my first script”

3. From the File menu, click Save As.

4. Click My Documents.

5. In the File Name box, type test.vbs (it is not case sensitive).

6. Click Save.

7. If a dialog box appears about overwriting an existing file, click OK.

8. From the Notepad File menu, click Exit.

9. Click Start, and then click Run.

10. In the Open box, type cmd, and then click OK.

11. The command prompt appears, and the working directory is C:\Documents and Settings\yourname>. For example, on my computer, it is C:\Documents and Settings\ed>.

12.Type dir and press ENTER. A directory listing appears, one of which should be My Documents.

13. Type cd “My Documents” and press ENTER.

Note: The quotation marks are required. The capital letters are not.

You will now be in the C:\Documents and Settings\yourname\My Documents> folder.

14. Type cscript test.vbs. Inside the cmd prompt, you should see my first script appear.

15. Press the up arrow one time, and you should see Cscript test.vbs appear on the command line.

16. Press the HOME key. You should see the cursor move to the beginning of the command line.

17. Press DELETE eight times until you have deleted Cscript (including the trailing space). This leaves you with only test.vbs in the Command Prompt window.

18. Press ENTER. Now you will see a dialog box that displays the message, my first script.

 

This procedure illustrates creating a script, saving a script, and running a script in the Cscript scripting host, and the Wscript scripting host. It also illustrates that the Wscript.Echo command has two modes of operation. Inside the Cscript scripting host, it displays on the command line. When running inside the Wscript scripting host, dialog boxes are displayed.

I sincerely hope this gets you started in the wonderful world of scripting, and that you stick with this fascinating and amazingly useful skill. (Don’t feel bad that it took you 21 times to get it working. When I first started programming, I spent hours screaming at the walls trying to get my first piece of code to compile. I was even typing things like computername and username in my code [obviously, it didn’t work because those are just place holders for your own computer name and your own user name]). Therefore, I can certainly understand frustrating documentation and confusing instructions. I have tried to keep those early experiences in mind when I wrote my five scripting books and as I write the Hey, Scripting Guy! Blog posts. I do not always succeed, but I do try. Please stay with us, and let me know if I forget my early trials and tribulations! I know you will get it on the next go around.


Can I Use VBScript on Windows 7?
 

Hey, Scripting Guy! Question

Hey, Scripting Guy!

I just purchased a laptop to assist in teaching myself VBScript scripting.  Am I able to do VBScript using Windows 7? I sure hope so! I have attempted to code and save a Notepad text with a .vbs extension with no results.

-- PM

 

Hey, Scripting Guy! AnswerHello PM,

VBScript will work on Windows 7. Here are a couple links for you. First, you may want to visit our Learn to Script page. Next, you will want to visit the Hey, Scripting Guy! archive and look for VBScript articles. You may want to look at the Script Repository and view the thousands of VBScript sample files we have stored Last, you may want to hang out on the Official Scripting Guys Forum.

As far as your specific problem with creating a VBScript file, one thing you might want to check is to see if you are showing file extensions in Windows Explorer. From Tools, click Folder Options. On the View tab, scroll about halfway down and clear the check box for Hide extensions for known file types. This is shown in the following image.

Image of the check box to clear

 

By default file extensions are hidden, and when you save a Notepad file as script.vbs, you are actually saving script.vbs.txt. When saving your script in Notepad, from File click Save As, and in the dialog box that appears, change the Save As Type to All Files (*.*). After you have done this, you should be able to run your script. Windows 7 also includes Windows PowerShell 2.0. If you decide to experiment with that, you will find links from our Learn page as well. 

 

Troubleshooting a VBScript Script

Hey, Scripting Guy! Question

Hey, Scripting Guy!

I read your recent post, but was unable to duplicate your script. Can you include a sample, especially for those of us who are learning the ropes? I have made a folder on the C:\ drive called FSO and added a text file with my local computer name written in it. However, I get an immediate error on line 1 of the VBScript code. What is wrong?

-- KS

 

Hey, Scripting Guy! AnswerHello KS,

The script in the December 31, 2009, Hey, Scripting Guy! Blog post is a Windows PowerShell script, not VBScript. To run the script, you will need to copy and save the script with a .ps1 extension. If you have Windows 7 or Windows Server 2008 R2, you already have Windows PowerShell installed, but you will need to enable Windows PowerShell script support by opening the Windows PowerShell console. Click Start, click All Programs, click Accessories, click Windows PowerShell, and then click Windows PowerShell ISE. Make sure Windows PowerShell ISE is running as an administrator (right-click Windows PowerShell ISE and then click Run as administrator).

On Windows Server 2008 R2, the Windows PowerShell ISE must be added as a feature.

In the bottom of the Windows PowerShell ISE, there is a command console. Type the following command:

Set-ExecutionPolicy remotesigned

A warning will appear that warns you about changing the execution policy. Read it, and then press Y. You can now close the Windows PowerShell ISE and re-open it with normal user rights.

Paste the script from the December 31, 2009, blog post into the top of the Windows PowerShell ISE pane, and then click the green Run Script button (or you can press F5).

If you do not have Windows 7 or Windows Server 2008 R2, you can download and install Windows PowerShell 2.0. I have written a number of Hey, Scripting Guy! Blog posts that talk about getting started with Windows PowerShell. We also have a good learning page for Windows PowerShell.

 

This concludes another edition of Quick-Hits Friday. It also concludes another exciting week on the Script Center. Join us next week as we delve into the mysteries of…we will let that remain a mystery for now.

If you want to know exactly what we will be looking at on Monday, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow for the Weekend Scripter, a brand new series on the Script Center. Check back with us tomorrow to see what all the fuss is about!
 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I want to starting scripting on the F5 BigIP Box. Where do I start learning. I have never script before.