Subscribe to Windows IT Pro
October 08, 2002 12:00 AM

How to Preload Commands in a Command Shell Window

Let the command shell window do all the work
Windows IT Pro
InstantDoc ID #26631
Rating: (2)
Downloads
26631.zip

Do you repeatedly use certain commands in your job? Rather than constantly retyping those commands, would you rather have those commands preloaded and available for use each time you open a command shell window? This capability isn't just wishful thinking. You can preload basic commands quite easily by taking advantage of the script CommandShellLoader.vbs. After you use this script to preload your commands, accessing and running them can be as simple as pressing your keyboard's Up Arrow or Down Arrow key, then pressing Enter. Even better, you can use one version of the script to preload one command shell window with the commands you use to manage users and groups, another version of the script to preload a window with the commands you use to manage share, file, and folder permissions, and so on. To keep track of the multiple commands you're running, the script changes the command shell window's color each time it runs.

CommandShellLoader.vbs uses Windows Script Host (WSH) and NTFS file streams to achieve these feats. To use this script, you need to know how to preload commands and change the color of command shell windows. Armed with this knowledge, you can adapt the script to preload the commands that you use most often.

Preloading Commands
Since the days of DOS and the Doskey command, administrators have enjoyed the convenience of using the Up Arrow and Down Arrow keys to access previously run commands. You can take advantage of this DOS feature by preloading commands that you use most often. To preload commands, you simply open a command shell window and run those commands so that they're in the command buffer.

To programmatically preload commands, you can use WSH, as the code in Listing 1 shows. (You can also use the Microsoft ScriptIt utility, which Microsoft doesn't support.) This VBScript code uses the WshShell object and its SendKeys method to preload the Dir command and the Ping /? command. The code opens a command shell window, enters the Dir and Ping /? commands, then executes them. Note the frequent use of the line

WshShell.SendKeys("~")

in Listing 1. This line is equivalent to pressing the Enter key. After you run the code in Listing 1, try pressing the Up Arrow key. As you can see, pressing the Up Arrow key reenters the commands in the command shell window, making them available for reuse without retyping them.

Let's look at a real-world example of how you can take advantage of preloading commands. At my job, I frequently perform user and group management tasks. I often use the Microsoft Windows 2000 Server Resource Kit's Findgrp tool to determine a user's group memberships. I use the Findgrp tool for two resource domains (Sales and Production) and two user domains (South and North). So, I use the following Findgrp commands:

Findgrp Sales South\UserID
Findgrp Sales North\UserID
Findgrp Production South\UserID
Findgrp Production North\UserID

where UserID is the ID of the user for whom I want to determine group membership. The only part that changes in these Findgrp commands is the user ID, so the Findgrp command works well for preloading. I preload the first section of the command (e.g., Findgrp Sales South\), then whenever I need to determine a user's membership, I press the Up Arrow key, type the user's ID, and press Enter to run the command without having to type the first section again.

In the Findgrp command, the element that changes is conveniently at the end of the command. How do you handle situations in which the changeable element falls in the middle of the command? You can still preload such commands if you use a bit of creativity. Take, for example, the Local command, which I often use to display the members of local groups. The syntax that I follow is

Local group_name
  domain_name | \\server

where group_name is the name of the group for which I want to see the members, domain_name is the name of the domain in which the group resides, and \\server is my server's name. Although the domain_name and \\server elements are static, the group_name element continually changes.

The code in Listing 2 shows how you can preload this command. As the code at callout A in Listing 2 shows, you first preload a command that sets the localgrp variable, then preload the Local command, which uses the localgrp variable. After you preload these commands, you simply have to press the Up Arrow key twice to access the "Set localgrp=" entry, enter the group name, press Enter, press the Down Arrow key to get back to the Local %localgrp% North command, then press Enter again. Because you set the localgrp variable to the group's name, the OS will substitute the specified group's name when it runs the command.

Changing Window Colors
I often have several command shell windows open simultaneously. Occasionally, I accidentally close the wrong window, killing a script that was still running. To help differentiate between tasks, I decided to launch windows that have different background colors. You can modify the background color of a command shell window in several ways:

  • You can manually change the background or text colors by right-clicking the command shell window's title bar, selecting Properties, then clicking the Color tab.
  • You can manually change the color settings when you create a shortcut that points to the \%systemroot%\system32\cmd.exe directory.
  • You can programmatically change the color settings with Windows shell scripting's Color command.

Related Content:

ARTICLE TOOLS

Comments
  • Anne
    4 years ago
    Sep 08, 2008

    Thanks for letting us know, bob; our customer service team has been notified about the problem. Due to popular demand, I've changed this article's access to Registered User for the next 30 days, so that non-VIP subscribers will be able to view the entire article and download the code.

  • Bob
    4 years ago
    Sep 08, 2008

    where is the rest of the article?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.