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.

zx485
  • 24,099
  • 26
  • 45
  • 52

2 Answers2

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
abelenky
  • 58,532
  • 22
  • 99
  • 149
  • 2
    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. –  Feb 19 '17 at 18:36
  • Thanks @abelenky I'll be sure to give that a try :) :) – Tristan Otterpohl Feb 19 '17 at 20:47
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
Community
  • 1
  • 1
eckes
  • 9,350
  • 1
  • 52
  • 65