Platform SDK: Active Directory, ADSI, and Directory Services |
Removing groups, like creating them, is performed from the group's parent object. Use the Delete method to remove groups, passing the class "group" and the name of the group to remove:
myComputer.Delete "group", myGroup.Name
Unlike creating a group, removing groups does not require a call to SetInfo. Once the Delete method is called successfully, the group is gone. In production code, make sure to enumerate the group's Members property to check if there are still any users in the group before deleting it:
For Each user In myGroup.Members numUsers = numUsers + 1 Next If numUsers > 0 Then WScript.Echo "This group still has users in it." End If
The following code removes the group "KillMe" from a local machine:
Dim myComputer Dim myTarget Set myComputer = GetObject("WinNT://mymachine") myComputer.Delete "group", "KillMe"
Remember, this example blindly deletes a group without worrying about its potential contents; safer code would include some user interaction to insure that nothing is deleted by accident.