Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 2007

Counting on For

A Windows bug provides fine fodder for more For functionality
RSS
Subscribe to Windows IT Pro | See More Active Directory (AD) Articles Here | Reprints
Or get the Monthly Online Pass—only $5.95 a month!

Last month, in "The Power of For" (InstantDoc ID 96539), I began a discussion about the For command, one of those little unsung Windows "hero"

tools. I showed you how to use For to make a program capable of processing wild cards even when that program doesn't understand wild cards. For example, to make the imaginary Processfile command process every file whose name starts with "z," I could type

for %a in (z*) do processfile %a 

In other words, For takes a program that processes one file at a time and transforms it into a program that can process a series of files—pretty useful functionality. However, last month I had room for only the merest of For's powers. Let's remedy that.

Couldn't Resist
A few years ago, Microsoft announced an entertaining bug—more like an Easter Egg—in the Windows Server 2003 and Windows 2000 Server versions of the Microsoft Management Console (MMC) Active Directory Users and Computers snap-in. Open the snap-in, navigate to any group, right-click the group's icon, and choose Properties. In the resulting Properties dialog box, you'll see a Members tab that shows all the members of that group, including an androgynous head icon for each user. But if a group has more than 500 members, the hair color on the icon goes from black to white. Of course, when I heard about this bug, I had to try it out. But how would I generate 501 user accounts?

In previous columns, I've explained that you can create a user account on a domain from the command line by typing

net user <username> <password> /add /domain 

However, typing a Net User command 501 times doesn't sound like fun. With For's /l option—which tells For to count—I can tell Windows to do 501 Net User reiterations in just one line. The syntax for For /l is

for /l %a in (<first number> <increment> <last number>) do 
  <command> 

For example,

for /l %a in (1 1 5) do echo %a 

would have the effect of telling For to show the numbers 1 through 5, incrementing by one. Armed with this functionality, I can then type

for /l %a in (1 1 501) do net user testuser%a  
  ComplexPassword$ /domain /add 

This command creates 501 user accounts with the names testuser1, testuser2, and so on up to testuser501. Each user has the same password—ComplexPassword$—and the accounts are created on the domain. (Please try this only on a test domain.) After the command has completed, open the Active Directory Users and Computers snap-in, find the Domain Users group, and examine its membership: You'll find all white-haired icons.

To get rid of these test accounts, you can use the

net user <username> /delete /domain 

command, but don't forget to use For to pump up its power to delete all 501 accounts, as follows:

for /l %a in (1 1 501) do net user testuser%a /domain  /delete

Getting Complex
Thus far, the command portion of For has been a single command (e.g., Chml last month, Net User this month). But what if you want to perform multiple tasks in one For command?

For example, suppose you want to not only create a user named testusernumber but also add that user to a domain local group called test. First, you can add a user to a domain local group with the command

net localgroup <groupname> <username> /add 

To simultaneously instruct For to add the user to the test group, you can put the two necessary commands on a single line by placing the ampersand character (&) between them and surrounding the two commands with parentheses. To create 501 users, then, and also add each one to the test group, you could type

for /l %a in (1 1 501) do (net user testuser%a
  ComplexPassword$ /domain /add & net localgroup test 
  testuser%a /add) 

Stay tuned for more For!

End of Article



Reader Comments
Need access to the online article...

slemmesmi November 06, 2007 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

VMware and the Future of Virtualization

What's next for virtualization and business IT? Windows IT Pro senior editor Jeff James speaks with VMware President and CEO Diane Greene on the future of virtualization technology. ...

Common .pst File Questions

Sue Mosher addresses some of the inevitable questions that Outlook users eventually ask about Personal Folders (.pst) files. ...


Active Directory (AD) Whitepapers An Introduction to Windows Server 2008 Server Manager

Get More from Active Directory—Easily Audit Changes, and Secure and Restore Objects

User Provisioning: Get the Most Bang for your IT Buck

Related Events Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

A Guide to Group Policy

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing