7

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
  • I've had a similar bug in the past, can't remember exactly, but I think it was resolved by removing random spaces in my script. For some reason cmd was adding unnecessary spaces because I have spaces elsewhere in my script. Try removing all spaces that aren't absolutely needed. I know it sounds random, but it worked in my case. Commented Feb 26, 2018 at 18:44
  • @IronWilliamCash exactly what I need to remove those spaces. I don't know how.
    – Aria Fathi
    Commented Feb 26, 2018 at 19:30
  • You can remove undesirable whitespace with this form of command, just add it to the appropriate part of the script: set "rdpgroup=!rdpgroup: = !" (it simply converts two spaces to a single space, but you can change it to for example convert 4 spaces to a single space, or to convert 3 spaces to no spaces -- there are many possibilities).
    – Ed999
    Commented Mar 26, 2019 at 6:56

2 Answers 2

8

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .