0

I am looking to implement a simple batch file that will rename the current local profile folder, backup registry keys then delete profile list SID keys. Therefore allowing the computer to create a new local profile that is not temporary.

Where %U% is the account name

set a=wmic useraccount where name="%U%" get sid /value

and so when I execute the below(Where %a% is the above command):

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%a% /f

it interprets it as:

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\wmic useraccount where name="%U%" get sid /value"

But I want:

"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\SID=S-1-5-21-3519583588-1143172139-1479499458-1001"

If I call %a%, it displays

SID=S-1-5-21-3519583588-1143172139-1479499458-1001

and if I echo %a%, it displays

wmic useraccount where name="%U%" get sid

If I just enter %a%, it displays

SID=S-1-5-21-3519583588-1143172139-1479499458-1001

Just an explanation for why this happens would be great.

2 Answers 2

2

set does not have the built-in ability to execute a command and store the result, as you intend with the line:

set a=wmic useraccount where name="%U%" get sid /value

Instead, a simple hack is:

@echo off
for /f %%A in ('wmic useraccount where "name='%USERNAME%'" get sid /value ^| findstr SID') do ( set %%A )
echo The SID is %SID%

After that, you should be able to:

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%" /f
Sign up to request clarification or add additional context in comments.

2 Comments

The response from the wmic command will be SID=.... so omit the SID= and simply set %%A. And since there is no other output the goto :br isn't necessary.
Thanks @abelenky I'll be sure to give that a try :) :)
0

Unlike Unix where you use backticks for an expression which evaluates to the stdout of an external command there is no such syntax in batches. What you need to do instead is to use a for loop over the output lines to assign it to a variable, like discussed here.

for /f "delims=" %%i in ('wmic...') do set A=%%i

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.