0
votes

I know there are other topics on here with an identical goal, but don't think there's one in batch file format so here goes :

I've done a simple reg query in a batch , to find the registry key containing a user profile.

set /p username= please enter the user name ?
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /f "C:\users\%username%" /d /s /e

this finds the exact key I'm looking for which is

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1908996837-4138030977-3423296585-49875

I then want to delete that key and all that is in it, but the output of the reg query command is actually giving 3 different results, which is making it difficult for me to use in a for loop.

Here's what it actually outputs to a text file

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1908996837-4138030977-3423296585-49875

ProfileImagePath REG_EXPAND_SZ C:\Users\Mr Smith

End of search: 1 match(es) found.

So the next line I have is a for loop which obviously, based on the output of the above command, is not quite doing what I want

for /F %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /f "C:\users\%username%" /d /s /e') do reg delete %%a /f

So the ultimate aim being to perform this

reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-1908996837-4138030977-3423296585-49875" /va /f

but obviously without knowing the and typing in the full key name

Thanks

2
Firstly, use a different variable than %username%. That's a reserved variable containing the username of the current logged on user. Next, this sort of thing makes me nervous. You're going to be leaving a bunch of orphaned profiles in HKU and directories in C:\Users. It's best to use the dialog resulting from rundll32.exe sysdm.cpl,EditUserProfiles to delete user profiles.rojo
thanks for the tip rojo, I only used that variable name for ease of example on here but good point. So is there any way i could still set a variable as the name of a user account, and then direct that variable to the edituserprofiles dialog? In otherwords, not really having to use the dialog at all and have it done automatically?Ian Laird

2 Answers

0
votes

The Net User command-line tool could help system administrators to add, delete or modify user accounts...

Net User /? output is too succinct, so learn more

0
votes

Sorry to bump an old post, but I had the same Q. Here is your A:

set /p targetUser=Please enter the user name: 

for /F "tokens=*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /f "C:\users\%targetUser%" /d /s /e ^| FIND /I "HKEY" ') do set "userRegKey=%%a"

reg delete "%userRegKey%" /va /f