VBS Script to disable a list of Active Directory user accounts

The below script will disable a list of Active Directory user accounts provided in a text file, the script could easily be modified to change any other properties for the list of users. I have this script scheduled to run every night to disable a list of supplier user accounts used for remote VPN access.

Change the values for  strUserList and strDomain as needed.

On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
Const ForReading = 1
 
strUserList = "c:\userlist.txt"
strDomain = "dc=yourdomain,dc=co,dc=uk"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strUserList, ForReading)
 
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
	arrLine = Split(strLine, ",")
	strAccount = arrLine(0)
	If LCase(strAccount) <> "samaccountname" Then	
		Set objConnection = CreateObject("ADODB.Connection")
		Set objCommand =   CreateObject("ADODB.Command")
		objConnection.Provider = "ADsDSOObject"
		objConnection.Open "Active Directory Provider"
		Set objCommand.ActiveConnection = objConnection
 
		objCommand.Properties("Page Size") = 1000
		objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
		objCommand.CommandText = "SELECT AdsPath FROM 'LDAP://" & strDomain & _
			"' WHERE objectCategory='user' AND samaccountname='" & strAccount & "'"  
 
		Set objRecordSet = objCommand.Execute
 
		objRecordSet.MoveFirst
 
		Do Until objRecordSet.EOF
			strUserPath = objRecordSet.Fields("AdsPath").Value
			Set objUser = GetObject(strUserPath)
			objUser.AccountDisabled = True
			objUser.SetInfo
			objRecordSet.MoveNext
		Loop
	End If
Loop
 
objFile.Close

2 thoughts on “VBS Script to disable a list of Active Directory user accounts”

  1. Hi,
    Sorry a bit of a bug in my script even though is says CSV the script just wants a flat list of accounts like the below;
    user1
    user2
    user 3
    One account per line
    Hope that helps?
    Phil

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.