Solved

# How do I delete user account from AD using vbscript and a spreadsheet with the accounts to remove

Posted on 2008-06-19
Medium Priority
1,890 Views
I need a vbscript that will look at a spreadsheet and then delete those accounts from Active Directory based on the user logon.
0
Question by:WNottsC
• 3

LVL 24

Accepted Solution

purplepomegranite earned 1500 total points
ID: 21820796
The attached should do what you want.

I have tested the Excel code part (just change the variables at the beginning of the script for your needs), but I have been unable to check the AD part due to not being on a domain right now (or having access to a lab domain), so this part needs testing.

It should work - it was taken from another script to delete user accounts that worked.  Let me know!
strSourceWorkbook="E:\ee\AD\Delete AD Accounts from Excel List\test.xls" ' The workbook containing the account list
strSourceSheet="Sheet1" ' Set this to the name of the sheet containing the username list
rngUsername="B2" ' Set this to the first cell containing username in Excel

On Error Resume Next

' Initialise Excel
set oExcel=CreateObject("excel.application")
if Err.Number<>0 then
Err.Clear
WScript.Echo "Failed to open Excel"
WScript.Quit
end if
oExcel.Visible=false
oExcel.DisplayAlerts=false ' Don't show any dialog box prompts

' Initialise connection

' Obtain usernames from Excel file
set srcWorkbook=oExcel.Workbooks.Open(strSourceWorkBook)
if Err.Number=0 then
i=0
wsclript.echo "Deleting user " & strUserName
i=i+1
wend
srcWorkbook.Close
else
wscript.echo "Couldn't open workbook: " & Err.Description
Err.Clear
end if
oExcel.Quit

' Close connection

set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strFilter = "(&(SAMAccountType=805306368)(SAMAccountName=" & strUsername & "))"

strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter & ";" & strFields & ";subtree"
wscript.echo "Deleting " & objRecordSet.Fields("distinguishedName").Value
Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)
objUser.Delete "user", "distinguishedName=" & objRecordSet.Fields("distinguishedName").Value
wend
end sub

0

LVL 24

Expert Comment

ID: 22007922
Thanks for the points, but why the B-grade?

http://www.experts-exchange.com/help.jsp#hi97
0

Author Comment

ID: 22008005
The solution was very good but I did have to provide a few changes in order to get this fully functioning.
0

LVL 24

Expert Comment

ID: 22008017
Thanks for clarifying.  Would it be possible to post the changes so that this question has a complete solution for anyone else looking for a similar function?  And also for my reference, of course! :-)
0

LVL 6

Expert Comment

ID: 22802688
I am looking for same request .. this script is not working

any update on this ??
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Today, unlike web development, the mobile landscape is complex enough for a software engineer and Android is posing more challenging environment thanks to its fragmentation issues on hardware and software fronts.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes
###### Suggested Courses
Course of the Month4 days, 1 hour left to enroll