Setting a variable based on a command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joshcarp1995
Posts: 1
Joined: 11 Oct 2017 06:35

Setting a variable based on a command

#1 Post by joshcarp1995 » 11 Oct 2017 06:44

Hello everyone, first time on this forums and was looking for some help with a batch file, im not an expert with these and trying to learn but I've got stuck. Basically I want the result of: wmic useraccount where name=josh.carpenter get sid
to be a variable I can use, this is what I tried:

Code: Select all

@echo off
echo
color 3f
mode con:cols=140 lines=70

for /f "SID=" %%i in ('wmic useraccount where name=josh.carpenter get sid') do set output=%%i

Echo %SID%

Pause


It returns: josh.carpenter - Invalid verb

Overall my goal is:

Code: Select all

@echo off
echo
color 3f
mode con:cols=140 lines=70

Set /p Username="Enter Users name (Firstname.Lastname)
for /f "delims=" %%i in ('wmic useraccount where name=%username% get sid') do set output=%%i

Echo %delims%

Pause


but that returns the same error

Any help would be greatly appreciated.

Thanks.
Last edited by Squashman on 11 Oct 2017 09:21, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Setting a variable based on a command

#2 Post by aGerman » 11 Oct 2017 10:11

%username% is already predefined. If you want the user to input something else then you should choose a different variable name.
Elsewise:

Code: Select all

@echo off
color 3f
mode con:cols=140 lines=70


for /f "tokens=1* delims==" %%i in (
  'wmic useraccount where "name='%username%'" get sid /value'
) do for /f "delims=" %%k in ("%%j") do set "output=%%k"

Echo %output%

Pause

Steffen

Post Reply