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



## fong21051 (Jan 19, 2008)

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

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


----------



## fong21051 (Jan 19, 2008)

below is part of the script that was modified for testing. Also, the new variable: strUserData, was not declared in a Dim statement, so it is added 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 (Jan 19, 2008)

Hi everyone,

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

Many thanks


----------



## fong21051 (Jan 19, 2008)

Please can someone kindly help me on this.

I just need the script I put in the first post amended to reflect my info 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 (Jan 19, 2008)

can anyone help me out on this? cmon guys any help or direction will do.
Thanks.


----------



## Squashman (Apr 14, 2005)

It is usually considered impolite etiquette when you continue to bump your thread to the top and your thread never left the first page of the forum category.

Don't you think someone would have posted if they knew the answer already.

A better option might be using some 3rd party tools like Adfind and Admod from JoeWare. These could easily be used to take care of deleting the users from the OU and you could use a simple RMDIR command in a batch file to remove the users Profile and Data folders.
Free Tools


----------



## fong21051 (Jan 19, 2008)

Sorry, I didn't do it on purpose. When I don't receive a response for several days, I simply login and then search and reply to my thread. I will check the position next time. My apologies


----------



## Jupiter2 (May 16, 2012)

I don't know but you do need to be the Adminstrator to make changes to an active directory.

At a guess, you might have to make the Active Directory, inactive or disabled, before it can be removed.


----------

