Need to delete a multiple users from the single group in AD
I need to delete a multiple users from the single group in AD. I have list of users in the text file.. Please guide us
October 14th, 2010 7:50pm

This can be done in a VBScript program. You would use the FileSystemObject to read the names in the file. You would bind to the group and use the IsMember method of the group object to make sure the user is a member, then use the Remove method to remove them. In both cases the method requires the ADsPath of the member. The file should have the Distinguished Names of the users. Assuming this, the program could be as folllows: Option Explicit Dim objFSO, strFile, objFile, strUserDN, objGroup Const ForReading = 1 ' Specify file of user Distinguished Names. strFile = "c:\scripts\users.txt" ' Bind to the group object. Set objGroup = GetObject("LDAP://cn=MyGroup,ou=Sales,dc=MyDomain,dc=com") ' Open the file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Read DN's from input file. Do Until objFile.AtEndOfStream strUserDN = Trim(objFile.ReadLine) ' Skip blank lines. If (strUserDN <> "") Then ' Check if user is a member of the group. If (objGroup.IsMember("LDAP://" & strUserDN) = True) Then ' Remove user from group. objGroup.Remove("LDAP://" & strUserDN) End If End If Loop ' Clean up. objFile.Close If your text file has the "pre-Windows 2000 logon" names of the users, you must use the NameTranslate object to convert into Distinguished Names. Then the script could be similar to below: Option Explicit Dim objFSO, strFile, objFile, strNTName, strUserDN Dim objTrans, strDomain, objGroup Const ForReading = 1 ' Constants for NameTranslate Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_NT4 = 3 Const ADS_NAME_TYPE_1779 = 1 ' Specify NetBIOS name of the domain. strDomain = "MyDomain" ' Specify input file of user NT names (pre-Windows 2000 logon names). strFile = "c:\scripts\users.txt" ' Bind to the group object. Set objGroup = GetObject("LDAP://cn=MyGroup,ou=Sales,dc=MyDomain,dc=com") ' Open the file for read access. Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) ' Use NameTranslate object to convert NT names to DN's. Set objTrans = CreateObject("NameTranslate") ' Initialize NameTranslate by locating Global Catalog. objTrans.Init ADS_NAME_INITTYPE_GC, "" ' Read NT names from input file. Do Until objFile.AtEndOfStream strNTName = Trim(objFile.ReadLine) ' Skip blank lines. If (strNTName <> "") Then ' Specify NT format of name. ' Trap error if name not found. On Error Resume Next objTrans.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strNTName If (Err.Number <> 0) Then On Error GoTo 0 Wscript.Echo strNTName & " not found" Else On Error GoTo 0 ' Retrieve RPC 1779 Distinguished Name. strUserDN = objTrans.Get(ADS_NAME_TYPE_1779) ' Check if user is a member of the group. If (objGroup.IsMember("LDAP://" & strUserDN) = True) Then ' Remove user from group. objGroup.Remove("LDAP://" & strUserDN) End If End If End If Loop ' Clean up. objFile.Close For more on using NameTranslate, see this link: http://www.rlmueller.net/NameTranslateFAQ.htm Richard MuellerMVP ADSI
Free Windows Admin Tool Kit Click here and download it now
October 14th, 2010 8:31pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics