Platform SDK: Active Directory, ADSI, and Directory Services

Associating Users With Groups

Changing group membership for users is a common system administration task, and simple to accomplish using ADSI.

Adding a User to a Group

To add users to groups, call the Add method of the group object, passing the ADsPath of the user to add. The following code adds the user "adsitest" to the group "Administrators":

Dim myGroup
Dim myUser

Set myGroup = GetObject("WinNT://mymachine/Administrators,group")
Set myUser = GetObject("WinNT://mymachine/adsitest,user")

myGroup.Add(myUser.ADsPath)

Note that unlike setting properties, no call to SetInfo is required to commit group membership changes to the directory.

Removing a User from a Group

To remove users from groups, call the Remove method of the group object, passing the ADsPath of the user to remove. The following script removes the user "adsitest" from the group "Administrators":

Dim myGroup
Dim myUser

Set myGroup = GetObject("WinNT://mymachine/Administrators,group")
Set myUser = GetObject("WinNT://mymachine/adsitest,user")

myGroup.Remove(myUser.ADsPath)

Again, notice that no call to SetInfo is required.