Platform SDK: Active Directory, ADSI, and Directory Services |
Removing users is a simple matter of calling the Delete method of the user's parent object. The following code binds to a user and removes it:
Dim myUser Dim userParent On Error Resume Next Set myUser = GetObject("WinNT://mymachine/username,user") If Err = &H800401E4 Then WScript.Echo "User not found" WScript.Quit(1) End If Set userParent = GetObject(myUser.Parent) userParent.Delete "user", myUser.Name Set myUser = Nothing
Setting myUser to Nothing in the last line is not strictly necessary in this script, since there is no code here that calls myUser after it has been deleted. In a longer script that might attempt to access myUser later in the code, setting deleted objects to Nothing is necessary to prevent errors.
Note the use of On Error Resume Next and the If Err statements to handle the case where the given username does not exist. For more information about trapping and handling ADSI errors, see Errors and Error Trapping.