CLI 101 – FOR

I’ve long maintained that the FOR command is one of most basic commands every administrator should know. I have a short tutorial you can download at

Here are some other examples on using the FOR command. Let’s say you have some command line utility that will take a computer name as a parameter, such as ping, and you want to run it against a list of computers. List the computer names in a text file like servers.txt. Then open a command prompt where the file is and run:

FOR /F %x in (servers.txt) do @ping -n 1 %x

If you want to verify you have the basic syntax correct, run this:

FOR /F %x in (servers.txt) do @Echo %x

This should display each server name in the list.

If you want to save the output to a text file for any command you can use console redirection, like this:

FOR /F %x in (servers.txt) do @ping -n 1 %x >>pingresults.txt

Be sure to use >> and not > or you will only get results for the last computer in the text file.

If you want to take the FOR command and put it in a batch file, then remember to use %% instead of %. Here’s a quick batch file version of the ping command example. To avoid errors, I recommend specifying the full path to the text file.

@echo off
REM Delete pingresults.txt if it already exists so we get a new log
if Exist c:\scripts\pingresults.txt DEL c:\scripts\pingresults.txt
FOR /F %%x in (c:\scripts\servers.txt) do @ping -n 1 %%x >>c:\scripts\pingresults.txt

IMPORTANT: The FOR command is only case sensitive when it comes to variables. %x is different than %X. If you run:

FOR /F %x in (servers.txt) do @Echo %X

You will not get the servers but rather %X.

Technorati Tags:

Advanced VBScript will make you smart

I know this is a shameless plug, but I couldn’t resist.

Monad and Event logs

Here’s another nifty Monad example from The Lazy Admin on using MSH to review Event logs.

Managing the Event Logs with MSH – The

My first Monad Script

Monad Script Screen Shot
I’ve started testing the waters with the latest build of MSH/Monad. There is of course a nascent Monad script hub at the Technet Script Center. I took inspiration from there and came up with a quick shell script to display services in on the local system. Services that are running are displayed in Green and stopped services display as Red. The script displays the Caption and ServiceName; results are sorted by Caption which is very easy to do in Monad.

The screen shot gives you and idea of the final result.

I plan on doing much more with Monad and MSH in the coming months so I hope you’ll keep my bookmarked or added to your RSS feed. (Add to your RSS subscriptions)

# ShowServices.msh
# stopped services will display in RED

# running services will display in GREEN

$colItems=get-wmiobject -class “Win32_Service” -namespace ` “root\CIMV2”
-computername $strComputer | sort “Caption” `
| write-object

foreach ($objItem in $colItems) {

if ($objItem.State -eq “Running”) {
write-host $objItem.Caption “(“$objItem.Name”)” `
-foregroundcolor “green” }

else {write-host $objItem.Caption “(“$objItem.Name”)” ` -foregroundcolor “red” }
# End Script

Advanced VBScript for Windows Administrators Published

According to my Microsoft Press project editor, Advanced VBScript for Microsoft Windows Administrators, is now published. Many online sellers are slowly updating their databases. You can now order the book although I can’t guarantee what the shipping situation might be. It might be 4-6 weeks before you see the book on a shelf at places like Borders or Barnes & Noble. I know I’m looking forward to going into a store and seeing my book on the shelf.

I hope you enjoy the book and ask that you post honest reviews at your favorite online bookseller. If you can post a short comment here as well, I’d appreciate it.

I’m not sure if there will be an “official” support site for the book, but feel free to post any questions about the script examples or content in the support forums at (Free registration is required to post)