Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Disable user in AD

Disable user in AD

Disable user in AD

Can someone guide me how to disable the user instead of delete the user

Sub DelUser

Dim objOU

Set objOU = GetObject(strOU)

objOU.Delete "User", "cn=" & strUserCN & "" ' instead of delete, I need to disable user

Set ObjOU = Nothing

strUserCN = ""

End Sub

RE: Disable user in AD

Googling "vbscript disable account active directory" yields many results. Which of those have you tried?

RE: Disable user in AD

Ok. This is the code I am using. I am not an expert in coding. just using a code available and tweaking as per my requirement. Tried different methods like .accountdisabled = True, but it didn't worked. Hope you can assist me to complete this.

'Script deletes Users from a csv file.
'csv format is strsAMUserName,Whatever
'Written by Andrew hill and Carl Harrison - Microsoft MCS
'this script is offered with no warranty
'On Error Resume Next 'used in case user not found

Option Explicit

Const ForReading = 1
Const ForWriting = 2

Dim strL, spl1, strOU, strUserCN, strName
Dim objFSO, objInputFile,objOutputFile

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("c:\Temp\dn.txt", ForReading) 'your csv file
Set objOutputFile = objFSO.OpenTextFile("c:\Temp\status.txt", ForWriting, True) 'your output file

'wscript.echo "script started"

'extract from csv file

Do until objInputFile.AtEndOfStream
strL = objInputFile.ReadLine
spl1 = Split(strL, ",")
strName = (spl1(0))
If UserExists(strName) = True Then

'WScript.Echo strName & " exists."
objOutputFile.WriteLine "UserID " & strName & " disabled "

End If


Set objFSO = Nothing
Set objInputFile = Nothing

wscript.echo "script finished"

'user exist check

Function UserExists(strsAMUserName)

Dim strDNSDomain, strFilter, strQuery
Dim objConnection, objCommand, objRootLDAP, objLDAPUser, objRecordSet

UserExists = False

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
Set objRootLDAP = GetObject("LDAP://RootDSE")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
'objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

strDNSDomain = objRootLDAP.Get("DefaultNamingContext")
strFilter = "(&(objectCategory=user)(sAMAccountName=" & strsAMUserName & "))"
strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter & ";sAMAccountName,adspath,CN;subTree"

objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

If objRecordSet.RecordCount = 1 Then


'WScript.Echo "We got here " & strsAMGroupName
'WScript.Echo objRecordSet.Fields("sAMAccountname").Value
'WScript.Echo objRecordSet.Fields("adspath").Value

If objRecordSet.Fields("sAMAccountname").Value = strsAMUserName Then
UserExists = True
Set objLDAPUser = GetObject(objRecordSet.Fields("adspath").Value)
strOU = objLDAPUser.Parent
strUserCN = objRecordSet.Fields("CN").Value

End If


'WScript.Echo strsAMUserName & " User doesn't exist or Duplicate sAMAccountName"
objOutputFile.WriteLine "UserID " & strName & " not found "
UserExists = False

strUserCN = ""
strOU = ""

End If

Set objConnection = Nothing
Set objCommand = Nothing
Set objRootLDAP = Nothing
Set objLDAPUser = Nothing
Set objRecordSet = Nothing

end function

Sub DelUser
Dim objOU
Set objOU = GetObject(strOU)
objOU.Delete "User", "cn=" & strUserCN & ""
'objOU.Delete "User", "cn=" & strUserCN & "" ' instead of delete I want to disable the user

WScript.Echo strName & " (CN=" & strUserCN & ") has been deleted."
Set ObjOU = Nothing
strUserCN = ""

End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close