I know this is an old post, but the other day I had a dire need to clean up a whole bunch of servers, I finally got a chance to look at this again and I did eventually get a working script, I thought I'd share.
This modified this from the examples in the Help file.
What it does:
- msgbox to remind YOU to log off of the server if you're on it before running, then launches inputbox for server name.
- This will search the registry for Roaming Profile users.
- It grabs their User ID and SID from the regkey
- Delete their Users folder (\\server\c$\users\UserID)
- Deletes the ProfileList reg key once their user folder is cleaned up.
- Deletes the recycle bin files, once all user folders are cleaned up,
Work to do:
- There are still other hooks tied to the SID (but not the user name) in the registry. Eventually i'd like to get those as well, but initial attempts searching the Registry for occurrences of the SID seem to take an excruciatingly long time, so I omitted those for now. Trying to determine if the entries are always in a particular location, then can hard set those locations to delete based on location which would be much faster than searching everything.
- Add all of this inside another parsed loop if you need to do multiple servers in succession by entering them into the InputBox separated by a comma. (currently we can only afford to take out one server from the farm at a time but might need to do multiples autonomously in the future).
Code: Select all
MsgBox, 1, Server User Disk Space Clean-Up, If you click OK you will be asked to specify a server.`n*** YOU MUST LOG OFF OF THE SERVER BEFORE INITIATING THE SCRIPT ***`n`nAfter entering a server name, the script will proceed to delete the \\server\c$\User\[TheUser] folders along with the associated ProfileList RegKey entry.`n`nClick Cancel to abort.
IfMsgBox, Cancel
return
SetBatchLines -1 ; Makes searching occur at maximum speed.
RegSearchTarget = FileServer ; Tell the subroutine what to search for. this is part of the path where roaming profiles are stored such as \\Fileserver\Users\etc.
InputBox, server, Enter a server name, Please input the server name that you want to clean up profile/disk space for.
server = \\%server%
Gosub, RegSearch
; Delete the Recycle Bin files from the server after looping through all the user folders
RunWait, %comspec% /c rd %server%\c$\$Recycle.Bin /s /q
gosub, RemoveTrayTip
MsgBox, User Cleanup has completed!
return
RegSearch:
ContinueRegSearch = y
; Fine the User profile List file specifically
Loop, Reg, %server%\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList, KVR
{
Gosub, CheckThisRegItem
if ContinueRegSearch = n ; It told us to stop.
return
}
CheckThisRegItem:
if A_LoopRegType = KEY ; Remove these two lines if you want to check key names too.
return
RegRead, RegValue
if ErrorLevel
return
IfInString, RegValue, %RegSearchTarget%
{
StringTrimLeft, UserSid, A_LoopRegSubKey, 57 ; isolate the SID for later user
Stringleft, SidPath, A_LoopRegSubKey, 56 ; Path up to but not including the SID
RegRead, userdir, %server%\%A_LoopRegKey%, %A_LoopRegSubKey%, ProfileImagePath
StringTrimLeft, userdir, userdir, 3
theuser = %userdir%
userdir = %server%\c$\%userdir%
TrayTip, Delete status:, InProgress: %userdir%`nPending: %UserSid%, 10, 1
; Delete User Directory - 2 different ways to do it.
FileRemoveDir, %userdir%, 1
; Check for duplicate folders. If you have temp or locked profile problems and duplicate user folders are created.
IfExist, %userdir%.SOMETHING ; Where "something" is the naming convention that is automatically created. (user.domain or user.domain.001 etc)
{
TrayTip, Delete status:, InProgress: %userdir%.SOMETHING`nPending: %UserSid%, 10, 1
FileRemoveDir, %userdir%.SOMETHING, 1
}
TrayTip, Delete status:, Completed: %userdir%`nInProgress: %UserSid%, 10, 1
; Delete Reg key
RegDelete, %server%\%A_LoopRegKey%\%SidPath%\%UserSid% ; works
;~ RegDelete, %server%\%A_LoopRegKey%\%SidPath%\%UserSid%.bak ; Delete .BAK entries
TrayTip, Delete status:, Completed: %userdir%`nCompleted: %UserSid%, 10, 1
}
return
RemoveTrayTip:
SetTimer, RemoveTrayTip, Off
TrayTip
return