6May/090
One line administration – search and delete computer accounts
Here’s a quick one-line script I’ve been using to clean up some old computer accounts in active directory. It requires the dsquery and dsrm tools from the Server 2003 Admin Pack or the Remote Server Administration Toolkit.
@for /f %%a in ('DSQUERY COMPUTER -NAME *%1') do (dsrm %%a)
Save that in as a batch file and it’ll query Active Directory for computers including the argument passed. Here’s an example with a naming structure where we have the last 4 digits of the MAC address in the name for uniqueness.
C:\>adrm.cmd 6736
C:\>(dsrm "CN=LABPC6736,OU=Labs,DC=yakk0,DC=org" )
Are you sure you wish to delete CN=LABPC6736,OU=Labs,DC=yakk0,DC=org (Y/N)? y
dsrm succeeded:CN=LABPC6736,OU=Labs,DC=yakk0,DC=org