Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

fong21051

Technical User
Jun 1, 2012
38
GB
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

 
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.
 
Hi everyone,

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

Many thanks.
 
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.
 
can anyone help me out on this? cmon guys any help or direction will do.
Thanks.
 
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
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.
 
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
 
Look at line 9, you are appending the string "ou=test staff leavers,dc=rcm,dc=ac,dc=uk" twice
 
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
 
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
 
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[highlight]\[/highlight]" & objUser.sAMAccountName
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top