Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

July 30, 2003 12:00 AM

Command-Line Scripting Tools in Windows 2003

The new OS version incorporates many new tools
Windows IT Pro
InstantDoc ID #39537
Rating: (1)

Using command shell scripts has always been one of the best ways to manage a Windows server, but writing flexible scripts often requires extra command-line tools from the OS support tools, a resource kit, or a third-party vendor. Each new version of Windows has incorporated some of these tools into the base OS. Windows Server 2003 incorporates more than 60 new command-line tools. Here are my 10 favorite new scripting-oriented command-line tools in Windows 2003.

10. Choice
A longtime staple of the Windows resource kits, the Choice command prompts the user to choose an available option and returns the user's input to the script in the ERRORLEVEL environment variable. For example, to ask the user to select option 1, 2, or 3, you'd type

choice /c:123 Select an option

9. Clip
The Clip command lets you redirect a command's output to the Windows Clipboard or send the contents of the Clipboard to a command. For example, to redirect the Dir command's output to the clipboard, you'd type

dir | clip

8. Setx
The Setx command sets an environment variable according to values supplied in the script, in a file, or even in a registry key. For example, to set the MACHINE environment variable to the value Dell Precision 610, you'd type

setx machine "Dell Precision 610"

7. Waitfor
The Waitfor command synchronizes the actions of multiple systems on a network. Waitfor can pause a script until the command processor either receives a specified signal or sends a signal to one or more systems on the network. The following commands wait for the Batchdone signal, then use the /si switch to send the signal:

waitfor Batchdone
waitfor /si Batchdone

6. Timeout
Similar to the Pause command, the Timeout command pauses the execution of a script for the specified number of seconds. To pause a script for 10 seconds, you'd type

timeout 10

5. Systeminfo
The Systeminfo command outputs basic system information to a file or to the standard output device. The information that Systeminfo returns includes the host name, OS version, BIOS version, processor type, system uptime, Windows directory, total system memory, and logon server. For example, to send system information to the mysystem.txt file, you'd type

systeminfo >> mysystem.txt

4. Where
The Where command returns the location of all files that match a specified search pattern. To find all .inf files that reside in the C:\winnt directory, you'd type

where /r c:\winnt *.inf

3. Forfiles
The Forfiles command executes a command for each file that matches the supplied search criteria. You can use wildcards in filenames and search on dates. In the command to be executed, you can also use a special set of variables that contain environment information such as the filename, file date, and size. The following command displays the filename, date, and size for all files older than 5 days:

forfiles /d -5 /C "cmd /c echo @fname @fdate @fsize"

2. Eventcreate
The Eventcreate command writes events to the event log. Eventcreate improves on the Logevent utility. The following command adds a custom log entry to the Application log:

eventcreate /t error /id 10 /l 
application /D "MyScript ended with an error"

1. Eventquery
The Eventquery command lets you extract selected entries from one or more event logs, including logs on remote systems. Unlike the other utilities I discuss, Eventquery is a Windows Script Host (WSH) script, so you need to use cscript.exe to run it. The following command filters the Application log and looks for the custom log entry that I used Eventcreate to create:

cscript c:\windows\system32eventquery.vbs /l application /fi "id eq 10"

Related Content:

ARTICLE TOOLS

Comments
  • Paul
    7 years ago
    Dec 22, 2005

    The comments by Brian were useful as it gave me the link to where to look for the list of cmd tools that can be used.
    thanks Brian

  • Brian Hulse
    8 years ago
    Apr 22, 2004

    This may be a little late, but the help file for CMD.EXE has a "New command line tools sections that lists them all.
    Use this cmd file to view:
    @echo off
    :: Shows Command processor help for Windows XP
    :: Set the properties on your link to this to run Minimized
    start hh.exe ms-its:%WINDIR%\\Help\\ntcmds.chm::/ntcmds.htm
    exit

  • israel shnaidman
    8 years ago
    Feb 02, 2004

    Some of these are very useful. I never understood why Microsoft removed CHOICE from batch commands.
    Could you please publish a list of all the new command line tools or give a link to this info.

  • israel shnaidman
    8 years ago
    Feb 02, 2004

    What's the difference between
    setx machine "Dell Precision 610"
    and
    set machine=Dell Precision 610

  • K Henriksen
    8 years ago
    Jan 09, 2004

    Using the Where command in the above example does nothing more than the command "dir /b c:\\winnt\\*.inf /s". Where is the advantage in the Where command? I don't expexct this to be posted, I am just wondering why where is in this list?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

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