Get output of a CMD command and set it to a batch variable

I run this command to get name of groups:

set "remotegroup=" for /f "skip=1delims=" %%a in ( '"wmic group where sid="S-1-5-32-555" get name"' ) do if not defined remotegroup set "remotegroup=%%a"

but when I do this echo "%rdpgroup%" the output is like this:

"Remote Desktop Users "

I dont want those spaces at the end of users. Just want an output like this: "Remote Desktop Users"

3

2 Answers

The output of WMIC is unicode !

The trailing <CR> can be removed by passing the value through another FOR /F loop. This also removes the phantom "blank" line (actually a <CR>)

@echo off
set "remotegroup="
for /f "skip=1 delims=" %%a in ('"wmic group where sid="S-1-5-32-555" get name"') do ( for /f "delims=" %%b in ("%%a") do if not defined remotegroup set "remotegroup=%%~nb"
)
echo "%remotegroup%"
pause
0

Different ways to get the same results By ThunderJun:

OP1: Here we use setlocal to activate the use of special variables of the delayed type (!var!) Without using a for within another for. To get the same result.

OP2: Here we assign the command in a common type variable and with the help of vertical bar, we combine several commands, always taking priority first. We add the filter: more +1 to indicate to ignore the first output line and then add the filter: findstr /i "remot" with another vertical bar, with this we are indicating, that only print lines that contain the keyword that is in quotes and that does not distinguish between upper or lower case letters. Then we use a type variable common indicating to it, not to show the last 3 characters: %var:~0,-3% In this way, we get the same result without using a for within another for.

OP3: Here we use setlocal, to activate the use of special variables of delayed type (!var!). With a vertical bar, at the end of the command inside the for. For it, we assign a filter with the command: findstr /i "remot" indicating that it only prints lines with the keyword in quotes and that does not distinguish between upper and lower case letters. We use a special variable of delayed type, to capture the command output and we indicate replace 2 characters of spaces for nothing (!var: =!), to achieve the same result without using a for inside from another to.

@echo off
:::::::::::::OP1 START. BY ThunderJun
setlocal enabledelayedexpansion
set remotegroup=
for /f "skip=1 delims=" %%a in ('"wmic group where sid="S-1-5-32-555" get name"') do (
set remotegroup=%%a!remotegroup:~0,-4!
set rio=!remotegroup:~0,-1!
set remotegroup=!rio:~0,-1!
)
echo OP1 "%remotegroup%"
:::::::::::::OP1 END
:::::::::::::OP2 START. BY ThunderJun
set a='"wmic group where sid="S-1-5-32-555" get name |more +1 |findstr /i "remot""'
for /f "delims=" %%c in (%a%) do (set remotegroup=%%c)
echo OP2 "%remotegroup:~0,-3%"
:::::::::::::OP2 END
:::::::::::::OP3 START. BY ThunderJun
setlocal enabledelayedexpansion
set remotegroup=
for /f "delims=" %%a in ('"wmic group where sid="S-1-5-32-555" get name |findstr "remot""') do ( set remotegroup=%%~na& set remotegroup=!remotegroup: =!)
echo OP3 "%remotegroup%"
:::::::::::::OP4 END
pause
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like