There are times when you need to delete user accounts, and with ADSI you can very easily delete large numbers of users with a single click of the mouse. Some reasons for deleting user accounts are:
Set objDomain = GetObject(provider & ou & domain)
objDomain.Delete oClass, oCn & oUname
If you modify the CreateUser.vbs script, you can easily transform it into the DeleteUser.vbs script, which follows. Notice that the Reference information section is basically the same. It holds the path to the OU and the path to the user in the variables, enabling you to modify the script more easily. The main change is in the Worker section of the script. The binding string is the same as seen earlier. However, you use the connection that was made in the binding string and call the Delete method. You specify the class of the object in the oClass variable in the Reference section of the script. You also list the oUname and cn= parts as well. The syntax is Delete(Class, target). The deletion takes effect immediately. No SetInfo command is required.
Option Explicit
'On Error Resume Next
Dim strProvider 'defines how will talk
Dim strOU 'path to where new object will be created
Dim strDomain 'name of strDomain connecting to
Dim strClass 'the class of object we are creating
Dim strOUname 'name of object are creating
Dim objDomain 'holds connection to adsi
Dim objOU 'holds handle to create method
strprovider = "LDAP://"
strOU = "OU=mred," 'when using is OU=mred, THE , would be required.
strDomain = "dc=nwtraders,dc=msft"
strClass = "User"
strOUname = "CN=MyNewUser"
Set objDomain = GetObject(strProvider & strOU & strDomain)
objDomain.Delete strClass, strOUname
If Err.number = 0 Then
WScript.Echo(strOUname & " was deleted")
Else If Err.number = "-2147016656" Then
WScript.echo strOUname & " does not exist"
Else
WScript.echo " error on the play " & Err.Number
End If
End If