DS commands and FOR loops

Good old fashion "ds" commands, basic but always available and just do the job! While now days we have PowerShell, I still do come across Win 2003 boxes. DS commands in combination with FOR loops are quite powerful.


How to query Active Directory for disabled computer accounts and move them to specified OU?


for /f "Tokens=*" %s in ('dsquery computer -disabled -limit 0') do (DSMOVE %s -newparent "ou=Disabled_Computers,dc=securesenses,dc=net")


How to move computers to a specified OU using list of hostnames? (hostnames must be listed on per line)

for /f "Tokens=*" %i in (comps.txt) do dsquery computer -name %i | (dsmove -newparent "OU=DBServers,OU=Servers,dc=securesenses,dc=net")


How to create security groups with descriptions listed in a text file?

for /f "delims=, tokens=1,2" %i in (groups.txt) do dsadd group "CN=%i,OU=Groups,DC=securesenses,DC=net" -desc %j


How to disable list of computers?

for /f "Tokens=*" %i in (comps.txt) do dsquery computer -name %i | dsmod computer -disabled yes


How to create user accounts from a list? 

for /f "delims=, tokens=1,2" %i in (accs.txt) do dsadd user "CN=%i,OU=SERVICEACCOUNTS,DC=securesenses,DC=local"  -display %i -samid %i -upn %i@securesenses.local -pwd %j -pwdneverexpires yes -canchpwd no