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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to delete AD accounts and all its userdata/profiles, but how to take ownership first?

Status
Not open for further replies.

fong21051

Technical User
Jun 1, 2012
38
GB
Hi everyone,
Is anyone able to assist me with this if possible. The script below works fine. I put the AD accounts to be deleted in the "To Delete" OU and it deletes all the corresponding profiles and userdata of those AD accounts in the "To Delete" OU from the Servers.
but the only issue is: I can only delete the files from the servers if I have "ownership" permissions of the "Profile" and "userdata" to be deleted.
Can someone help me amend the script below so I can automatically take ownership of these "folders and files" (on the Server) of the AD accounts I put into the "To Delete" OU. I have put in the "C drive" location in the script as a test.
Any help will be much appreciated!

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

Dim strOU, objOU, objFSO, objUser, strUserData

' Specify the OU.
strOU = "ou=To Delete,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:\profiles") 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:\userdata" & objUser.sAMAccountName
If (objFSO.FolderExists(strUserData) = True) Then
objFSO.DeleteFolder(strUserData)
End If

Next
----------------------------------------------
Many thanks.
Fong
 
Have you tried this ?
objFSO.DeleteFolder objUser.profilePath, True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Thank you for your quick response PHV, but i am a complete beginner programmer. The script above was mainly written for me so I won't know where to input the line objFSO.DeleteFolder objUser.profilePath, True
Are you able to assist if you have a moment?

Many thanks, Fong
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top