Removing user profiles from terminal servers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Removing user profiles from terminal servers

20 Jul 2015, 17:30

Hello,
I am trying to build something that will assist in managing user profiles within a large terminal server farm.
The intent is to use this for rebuilding user's profiles quickly, or for account terminations.

Goal:
Get the SID of a user from their domain name
Delete Registry key from HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%
Delete folder from c:\Users\%username%

Questions:
How do you get the SID of a different user?
Will FileDelete work with \\%servername%:HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%

Concept (certainly not working, but general idea of what I'm trying to put together):
Use Inputbox to simply enter user domain name into
save variable, query ASDI edit (or something else?) to get user SID.
Premade comma separated value of server list
loop server list looking for reg key and user folder and delete them

Code: Select all

InputBox, username, Profile Remover, Enter the user's domain name.
servername = server1,server2,server3 ; many more servers, but you get the idea

; This part, I am unsure how to accomplish
;         find SID and create %userSID% from %username%

loop, parse, servername , `,
{
filedelete, \\%servername%:HKEY_LOCAL_MACHINE, software\microsoft\windows nt\currentversion\profilelist\%userSID%
filedelete, \\%servername%\c$\Users\%username%
}
Thanks in advance for looking. :)
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Removing user profiles from terminal servers

20 Jul 2015, 20:49

AlleyArtwork wrote:Will FileDelete work with \\%servername%:HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\profilelist\%userSID%
no, please see RegDelete
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

21 Jul 2015, 08:50

TLM, Thanks! I completely missed RegDelete when looking at the RegRead help.
As for the user SID, I believe I can comspec with the following:
wmic useraccount where (name='username' and domain='currentdomain') get name,sid
I'm one step closer!
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

21 Jul 2015, 09:33

I believe this is the final working version.
I'll probably put in a tooltop or something to keep tabs on progress, or use a gui instead of input box to display status in a GUI window, but for functionality, this seems to work.

Code: Select all

InputBox, username, SID Finder, Enter the user's domain name.
RunWait, %comspec% /c wmic useraccount where (name='%username%' and domain='BRGL') get name`,sid > %a_mydocuments%\SID.txt, , hide
filereadline, sidline, %a_mydocuments%\SID.txt, 2
loop, parse, sidline, %a_space%
{
field%A_Index% = %A_LoopField%
}
userSID = %field3%

servername = server1,server2,server3,server4,server5,server6,server7 ; etc.
loop, parse, servername, `,
{
	RegDelete, \\%A_LoopField%:HKEY_LOCAL_MACHINE, software\microsoft\windows nt\currentversion\profilelist\%userSID%
	FileRemoveDir, \\%A_LoopField%\c$\users\%username%, 1
}
This could also be handy for removing those pesky .BAK profiles in a fell swoop!
Last edited by AlleyArtwork on 29 Jul 2015, 21:40, edited 1 time in total.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Removing user profiles from terminal servers

22 Jul 2015, 16:25

Glad that helped. You don't have to create a txt file to get the userSID.
Simply use WMI and WQL via COM

Code: Select all

For item in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_UserAccount Where name = '" username "' and domain = 'BGRL'")
    userSID := item.name

msgbox % userSID ; << for display purposes only
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

29 Jul 2015, 21:40

Thanks again. I didn't know how to do it this way, only how to output to cmd to txt file, then parse that file for the info needed. Happy not to have to write to a file just for that. I am probably doing many things inefficiently, but always excited to try it a new way!
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

30 Jul 2015, 09:43

Followup Question:
Am I missing an option or using the wrong option for removing the directories, or is it expected to take this amount of time to perform this action?

The deletion of the directory seems to take an excessively long time (500 to 700 seconds) during the removal of the user files from the terminal servers: FileRemoveDir, \\%A_LoopField%\c$\users\%username%, 1

Manually deleting the profile from User Profiles (after the slow loading Profiles window populates from Computer properties > advanced > profile settings) is fast after the Profiles load.
It is still faster to do this manually on each server than through the script even with 15 servers in the farm. I was hoping the automation would ensure no chance of human error (forgetting a server) and be faster than manually removing profiles, but with what I have, this isn't the case.
I am probably doing it wrong, but that's why i'm in the Ask for Help section ;)
User avatar
AlleyArtwork
Posts: 44
Joined: 09 Jun 2015, 21:08

Re: Removing user profiles from terminal servers

02 Aug 2016, 10:52

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Draken, Google [Bot], haomingchen1998, LepG and 267 guests