Q. How can I delete from Active Directory (AD) user accounts that are listed in a file?

John Savill

September 8, 2004

1 Min Read
ITPro Today logo

A. To delete the accounts listed in the file that I created in the FAQ, "How can I create a file that contains all user profiles that were created before a specific date?" (FAQ), ), I first created a text file that included information in the following format:

|[optional info after the pipe]|[optional info after the pipe]

etc.

For example:

CN=test1,OU=testing,DC=demo,DC=local|6/2/2004 10:59:32 PMCN=test2,OU=testing,DC=demo,DC=local|6/2/2004 10:55:14 PM

A pipe character (|) must follow the account's distinguished name (DN); the script ignores what follows the pipe.

I then wrote the delusersfromfile.vbs script, which deletes the accounts listed in the file. You can download the script at Code. Save the script as delusersfromfile.vbs. Remember to modify the script to include information specific to your installation.

Option ExplicitDim strFilePath, objFSO, objFilesTarget, sUser, objParent, sLine, aLine, _   sDN, oUser' Check that all required arguments have been passed.If Wscript.Arguments.Count  required. For example:" & vbCrLf _& "cscript delusersfromfile.vbs c:tempUserList.txt"Wscript.Quit(0)End IfstrFilePath = Wscript.Arguments(0)Const ForReading = 1Set objFSO = CreateObject("scripting.filesystemobject")Set objFilesTarget = objFSO.OpenTextFile(strFilePath,ForReading,True)Do While objFilesTarget.AtEndOfStream  True    sLine = objFilesTarget.ReadLine    aLine = split(sline, "|",-1,1)    sDN = aLine(0)    On Error Resume Next    sUser = "LDAP://" & sDN    wscript.echo sUser    Set oUser = GetObject(sUser)    Set objParent = GetObject(oUser.parent)    objParent.Delete "User", (oUser.Name)LoopSet oUser = Nothing

To run delusersfromfile.vbs, at a command prompt enter

cscript delusersfromfile.vbs c:templist.txt

You'll see output on screen that's similar to this:

Microsoft (R) Windows Script Host Version 5.6Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.LDAP://CN=test1,OU=testing,DC=demo,DC=localLDAP://CN=test2,OU=testing,DC=demo,DC=local

After executing delusersfromfile.vbs, you could run a script to verify whether the accounts have been deleted. For example, you could run the listusersolder.vbs script that I discuss in "How can I create a file that contains all user profiles that were created before a specific date?"; the list that the script outputs should be empty of old accounts.

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like