1

I'm trying to compile a batch file to get the userID of the current user, then use the output to delete a certain registry key.

I found this to get the SID of the current logged in user:

wmic useraccount where name='%username%' get sid

This worked perfectly and gave me:

SID

S-1-5-21-XXXXX-XXXXX-XXXX-XXX

Now I would like to use the S-1-5-21.... etc. To delete a certain registry key for the current logged in user, so the S-1-5-21 etc. How can I do that? Or is there an easier way, being able to determine current SID and delete a key consequently?

thank you!

2 Answers 2

1

Assuming you want to delete the registry key...

HKEY_USERS\[SID-OF-USER]\test

...this script should cut it:

@ECHO OFF
SET USERNAME=SomeUserName

:: retrieve user's SID, store in file. NOTE: a user can have more than 1 SID.
wmic useraccount where name='%USERNAME%' get sid | FINDSTR "^S-" > tmp.txt

:: load result into variable. NOTE: this will load the first line only.
SET /P SID_=<tmp.txt

:: remove spaces which turned out to be appended at the end.
SET SID=%SID_: =%

:: delete key in the registry (will probably required elevated privileges)
:: the /F causes reg.exe not to ask for confirmation
REG DELETE "HKEY_USERS\%SID%\test" /F

:: cleanup
DEL tmp.txt

I'm not aware of an easier way to "determine current SID and delete a key".

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late reply, I made a small variation on this and it worked perfectly! thank you!
1

Try this:

@echo off
wmic useraccount where name='%username%' get sid > temp.txt &more temp.txt > temp2.txt &del temp.txt
for /F "tokens=*" %%A in (temp2.txt) do set "userID=%%A"
del temp2.txt
echo %userID%
pause

You can use the %userID% variable after this code.

Note that you need to use the more because of the way wmic parses it's output

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.