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 


September 13, 2004

Windows Server 2003 Directory Service Tools

Work with AD objects from the command line
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!

Download the Code Here

As an administrator of an Active Directory (AD) domain, at some point you're going to want to perform some typical AD management tasks for multiple objects in one operation. You can automate many tasks in Windows 2000 if you're skilled in Windows Script Host (WSH) and Active Directory Service Interfaces (ADSI) scripting technologies, but bulk operations of common administrative tasks can be difficult without this knowledge. The Ldifde and Csvde tools provide limited support, but creating the required input files (i.e., LDAP Data Interchange Format—LDIF—and comma-separated value—CSV—files) can be tedious. Therefore, most people don't find these tools complete or easy to work with. Consequently, executing AD management operations for multiple objects under Win2K can be frustrating. Fortunately, Microsoft addressed this problem by providing a set of six directory service command-line tools in Windows Server 2003. Let's look at how to use these tools in some real-world situations that you might encounter.

6 Tools
The directory service tools are included with Windows 2003 or can be installed with the Windows 2003 Admin Pack. You can also install and run them on computers that are running Windows XP Service Pack 1 (SP1), but Microsoft doesn't recommend that you install the tools on Win2K computers. The tools can target Windows 2003 domain controllers (DCs) and Win2K DCs that have SP3 or later installed.

You use the six directory service tools to perform typical AD operations from the command line:

  • Dsadd adds a computer, contact, group, organizational unit (OU), quota, or user to a directory.
  • Dsget displays selected attributes of a computer, contact, group, OU, partition, quota, server, site, subnet, or user in a directory.
  • Dsmod modifies an existing computer, contact, group, OU, partition, quota, server, or user in a directory.
  • Dsmove moves any object (except a partition or quota) from its current location in the directory to a new location in the same domain and renames the object.
  • Dsquery uses specified search criteria to find a list of computers, contacts, groups, OUs, partitions, quotas, servers, sites, subnets, or users in a directory.
    This tool also uses the * parameter to perform Lightweight Directory Access Protocol (LDAP) queries.
  • Dsrm deletes from a directory any object that you specify on the command line.

In the balance of this article, I show you how to use the directory service tools from the command line. Of course, the challenge is to type these commands without any errors, but I alleviate this problem by providing CommandList.txt, which contains all the commands I use. So instead of typing the commands, you can download CommandList from the Windows Scripting Solutions Web site, cut the desired command from the file, and paste it at the Windows command prompt. (Go to http://www.windowsitpro.com/windowsscripting, enter 43753 in the InstantDoc ID text box, then click the 43753.zip hotlink.)

Creating Objects with Dsadd
Let's start by creating some users. Suppose we have a flat file containing the names of the users we want to create. For each user, the file lists the first name, a space, the last name, and a carriage return, as in the MyUsers file that Figure 2 shows. Using the Windows command shell's For statement in combination with Dsadd, we can create this set of users with one command:

For /f "tokens=1,2 delims= "

  %i in (MyUsers.Txt) do

  Dsadd user "cn=%j %i,ou=MyOU,

  DC=LissWare,DC=Net" -samid %i.%j

(Although the command has been split across multiple lines to fit the print newsletter format, you must type it on one line.)

The For /f statement uses a loop to parse the file content. Within the loop, the statement looks for the default space delimiter in each line, placing the first name (before the space) in the first token (%i) and the last name (after the space) in the second token (%j).

The Dsadd statement then uses the output of the For /f loop to create the users whose names are in the file. The command interpreter replaces %i with the first name and %j with the last name. As you can see, the first parameter specified for Dsadd is user, which specifies that we're creating a user object rather than a computer object, an OU object, or some other object type. The second parameter is the user's distinguished name (DN), which consists of an OU and a domain. If the OU doesn't exist, you can create it with a statement similar to

dsadd ou "OU=MyOU,DC=LissWare,

  DC=Net"

The last parameter, -samid, defines the sAMAccountName attribute and corresponds to the legacy logon name.

You can specify many more parameters on the command line, such as the first name (givenName attribute) and last name (sn attribute). Although you can set these properties at creation time, we'll perform this task in the next section of the article to give ourselves the opportunity to play with the Dsmod and Dsquery tools.

If you don't have much experience with command-shell commands, I recommend that you look at the online documentation provided about Windows 2003 installations. To access the documentation, open a command prompt and type

hh.exe %systemroot%\help
  ntcmds.chm

Modifying Objects with Dsmod
Now that we've created a few users, let's set some properties for them. To update the givenName attribute by using the -fn (first name) switch and the sn attribute by using the -ln (last name) switch, we use a command similar to the one we used to create the users:

For /f "tokens=1,2 delims= "


  %i in (MyUsers.Txt) do


  Dsmod user "cn=%j %i,


  ou=MyOU,DC=LissWare,DC=Net"


  -fn "%i" -ln "%j"
   Previous  [1]  2  3  Next 


Reader Comments
wondering what good my sub is when it simply takes me to other sub offers. How lame is that!
NOT happy...

nbmi-it June 22, 2006 (Article Rating: )


yeah it sucks... i want to see how to use dsquery.. i WINDOWS COMMAND LINE tool and i have to buy a stupid scripting subscription... i'll go somewhere else...

ahagman October 26, 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
Accessing Database Data with ADO

...

The Memory-Optimization Hoax

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

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...


Active Directory (AD) Whitepapers Sustainable Compliance: How to reconnect compliance, security and business goals

User Provisioning and Access Control

Managing Unix/Linux with Microsoft System Center Operations Manager 2007 Cross Platform Extensions Beta

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

Keeping Your Business Safe from Attack: Encryption and Certificate Services

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.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
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 Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 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