VBScript to Disable User AD Account Using sAMAccountName From .CSV File

Posted on 2008-06-16
Last Modified: 2013-12-23
Looking for a VBscript that will disable a group of user accounts in a W2K3 AD from a list of sAMAccountNames in a .csv file.  
-The CSV file will only have 1 column with the first cell titled "sAMAccountName" and all of names will follow in cells A2, A3, A4...  
-The .CSV file would located on the root drive of a local workstation that the script would be ran from and be titled "disabledaccounts".
-Finally, if possible, on every account that gets disabled, populate a note within each user account on the ADDRESS Tab, in the STREET field, that says "Account Disabled--Not Validated."
Is this possible?
Question by:azstbzimms
LVL 38

Accepted Solution

Shift-3 earned 350 total points
Comment Utility
Paste the script below into a text file with a .vbs extension.  Customize the value of the strCSV variable with the location of the CSV file.  Customize the value of the strDomain variable with the Distinguished Name of your domain.

Running the script will disable each of the users from the file and set their street address attribute to "Account Disabled--Not Validated."

On Error Resume Next


Const ForReading = 1

strCSV = "c:\userlist.csv"

strDomain = "dc=yourdomain,dc=local"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strCSV, 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


		Do Until objRecordSet.EOF

			strUserPath = objRecordSet.Fields("AdsPath").Value

			Set objUser = GetObject(strUserPath)

			objUser.StreetAddress = "Account Disabled--Not Validated."

			objUser.AccountDisabled = True




	End If



Open in new window


Author Closing Comment

Comment Utility
Thanks for your accurate and prompt solution!

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now