Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 2003

Command-Line Scripting Tools in Windows 2003

The new OS version incorporates many new tools
RSS
Subscribe to Windows IT Pro | See More Scripting Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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"

End of Article



Reader Comments
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?

K Henriksen January 09, 2004


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

israel shnaidman February 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 February 02, 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

Brian Hulse April 22, 2004


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

paulstonehewer December 22, 2005 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement