Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This forum is the most helpful site I've ever used. I used to use Deja.com; but, this site is better - hands down!..."

Geography

Where in the world do Tek-Tips members come from?

vbscript which deletes a user from the Active directory and all his data

fong21051 (TechnicalUser)
8 Jun 12 7:28
Hello,

I'm trying to write a vbscript which deletes all users from a specific Active directory OU, and all his data.Here's the info:

Profiledata location: \\lon-file\staffprof$\accountname

Userdata location: \\lon-file\accounts$\users\accountname

domainname: Lon.ac.uk

All the users to be deleted are in one specific sub OU (the location of OU: Lon.ac.uk > people > staff leavers > definite staff leavers).

I want the script to delete all the users in this "definite staff leavers" OU,and also to delete each of the corresponding user's profile and userdata from the location above.The user's profile and userdata is given the same name as the "user logon name" field in AD (in the "account" tab in the properties of the user in AD).

Is it possible to create a "lookup" so that it deletes the user profile and userdata first before deleting the account from AD?

The script below had answered a similar question in the pass. I will greatly appreciate if someone can spend the time to mofify the code according to my info above.
I spent a lot of time researching vbscript but I still find it tricky, e.g what the cn, dc is and how to specify the OU:

----------------------------------
This deletes all users in a specified OU, plus their home, profile, and TS folders (specified in AD):


Option Explicit

Dim strOU, objOU, objFSO, objUser

' Specify the OU.
strOU = "ou=Sales,ou=West,dc=MyDomain,com"

' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)

' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Filter on user objects.
objOU.Filter = Array("user")

' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Delete user profile path.
If (objUser.profilePath <> "") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete user TS profile path.
If (objUser.msTSProfilePath <> "") Then
If (objFSO.FolderExists(objUser.msTSProfilePath) = True) Then
objFSO.DeleteFolder(objUser.msTSProfilePath)
End If
End If
' Delete user home directory.
If (objUser.homeDirectory <> "") Then
If (objFSO.FolderExists(objUser.homeDirectory) = True) Then
objFSO.DeleteFolder(objUser.homeDirectory)
End If
End If
' Delete the user object from AD.
objUser.DeleteObject (0)
End If
Next

-----
If the folders are not specified in AD (homeDirectory, profilePath, and msTSProfilePath), then you can code to construct the folder names with code similar to:


' Delete userdata.
strUserData = "\\server\userdata$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If

---------------
below is part of the script that was modified for testing. Also, I notice that I did not declare the new variable I introduced, strUserData, in a Dim statement, so I added it here. The "definite staff leavers" OU is the OU that contains a few test leaver accounts and I have also created a few test folders. (Option Explicit requires that all variables be declared in Dim statements, which helps for troubleshooting to find typos):


Dim strUserData
For Each objUser In objOU
If (objUser.Class = "user") Then
Wscript.Echo "User: " & objUser.sAMAccountName
' Delete userdata.
strUserData = "\\server\userdata$\" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
Wscript.Echo "Delete Folder: " & strUserData
' objFSO.DeleteFolder(strUserData)
End If
' Delete the user object from AD.
' objUser.DeleteObject (0)
End If
End If

fong21051 (TechnicalUser)
8 Jun 12 7:32
Please may someone assist me in modifying the script according to my info:

Profiledata location: \\lon-file\staffprof$\accountname
Userdata location: \\lon-file\accounts$\users\accountname
domain name: Lon.ac.uk

All the users to be deleted will be moved into one specific sub OU. The location of OU: Lon.ac.uk > people > staff leavers > definite staff leavers.

At the moment this OU contains a few test accounts and I have also created a few test folders.

Will appreciate any help.
fong21051 (TechnicalUser)
10 Jun 12 16:44
Hi everyone,

Is someone able to assist me with this please. Any help is much appreciated!

Many thanks.
fong21051 (TechnicalUser)
13 Jun 12 7:36
Please can someone kindly help me on this.

I just need the script I put in the first post amended to reflect my below:

--------------------------------------------------------------
Profiledata location: \\lon-file\staffprof$\accountname
Userdata location: \\lon-file\accounts$\users\accountname
domain name: Lon.ac.uk

All the users to be deleted will be moved into one specific sub OU. The location of OU: Lon.ac.uk > people > staff leavers > definite staff leavers.

--------------------------------------------------------------

Is anyone able to help me with this? Many thanks.
fong21051 (TechnicalUser)
19 Jun 12 8:45
can anyone help me out on this? cmon guys any help or direction will do.
Thanks.
markdmac (MIS)
21 Jun 12 22:22
Did you test your delete modification? What were the results? Here is a simple example for you to test out. You will need to have earlier set UserName to be equal to the samaccountname.

CODE

Dim objFSO
Set objFSO = CreateObject("scripting.filesystemobject")

PathArray = Array("\\lon-file\staffprof$\","\\lon-file\accounts$\users\")

For Each Path In PathArray
Err = objFSO.DeleteFolder(Path & UserName,True)
If Err.Number <> 0 Then
WScript.Echo "Something went wrong deletign files"
End If
Next

I hope that helps.

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.

fong21051 (TechnicalUser)
27 Jun 12 6:28
Hi Mark,

Thanks for your response. I have tried to run the following as a .vbs file (I have created a "test profile" on my local PC in location: C:\profile test)

-----------------------------------------------------
Option Explicit

Dim strOU, objOU, objFSO, objUser

' Specify the OU.
strOU = "ou=test staff leavers,dc=rcm,dc=ac,dc=uk"

' Bind to the OU.
Set objOU = GetObject("LDAP://ou=test staff leavers,dc=rcm,dc=ac,dc=uk" & strOU)

' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Filter on user objects.
objOU.Filter = Array("user")

' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Delete user profile path.
If (objUser.profilePath <> "C:\profile test") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete user TS profile path.
If (objUser.msTSProfilePath <> "") Then
If (objFSO.FolderExists(objUser.msTSProfilePath) = True) Then
objFSO.DeleteFolder(objUser.msTSProfilePath)
End If
End If
' Delete user home directory.
If (objUser.homeDirectory <> "") Then
If (objFSO.FolderExists(objUser.homeDirectory) = True) Then
objFSO.DeleteFolder(objUser.homeDirectory)
End If
End If
' Delete the user object from AD.
objUser.DeleteObject (0)
End If
Next
---------------------------------------

But now I am receiving Windows script Host error code:

Line: 9
Char: 2
Error: 0x80005000
code: 80005000
source: (null)


Please can you help on this?
Many thanks,
Fong
guitarzan (Programmer)
27 Jun 12 10:33
Look at line 9, you are appending the string "ou=test staff leavers,dc=rcm,dc=ac,dc=uk" twice
fong21051 (TechnicalUser)
27 Jun 12 11:35
One word: AMAZING.
Thanks guitarzan, it worked and deleted the AD account and the file in location C:\profile test.
I will put in the location of the other stuff that needs to be deleted and test this.
But many thanks for your quick response!
Fong
fong21051 (TechnicalUser)
28 Jun 12 16:33
Hi again,

Thanks for all your help so far. I have run the script below and it deletes the AD account and also deletes the profile in location: C:\profile test
But it would not delete the userdata in the path I had specified: C:\pdrive test
Everything else deletes but this, and I receive no error messages. Please may you help.
Many thanks!

---------------------------------------------------------
Option Explicit

Dim strOU, objOU, objFSO, objUser, strUserData

' Specify the OU.
strOU = "ou=test staff leavers,dc=rcm,dc=ac,dc=uk"

' Bind to the OU.
Set objOU = GetObject("LDAP://" & strOU)

' Use FileSystemObject to delete folders.
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Filter on user objects.
objOU.Filter = Array("user")

' Enumerate users.
For Each objUser In objOU
' Skip computers (which have class user).
If (objUser.Class = "user") Then
' Delete user profile path.
If (objUser.profilePath <> "C:\profile test") Then
If (objFSO.FolderExists(objUser.profilePath) = True) Then
objFSO.DeleteFolder(objUser.profilePath)
End If
End If
' Delete the user object from AD.
objUser.DeleteObject (0)
End If

' Delete userdata.
strUserData = "C:\pdrive test" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If

Next
guitarzan (Programmer)
28 Jun 12 16:44
If the folder is called "C:\pdrive test", which contains a subfolder that matches objUser.sAMAccountName, then you should add a "\":

CODE

strUserData = "C:\pdrive test\" & objUser.sAMAccountName 
fong21051 (TechnicalUser)
28 Jun 12 17:51
WOW, simple fix but deadly. IT WORKS!!
Thank you so much for your help guitarzan man. I been trying to make it work for about 5 weeks now would never have suceeded without your help! Your the best man :D
Fong
guitarzan (Programmer)
28 Jun 12 19:24
Glad it worked for you.

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!

Back To Forum

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