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
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