Select Page
This entry has been published on 2016-01-13 and may be out of date.

Last Updated on 2016-01-13.

Scenario

According to a group membership of the current Windows workstation user, you want to run certain commands in a Windows Batch file (cmd, .bat).

 

Solutions

1)

Use file isMember.exe from here. The link also includes a usage example.

 

2)

If you do not trust foreign .exe files, create a file isMember.bat e.g. with this content (original script):

@echo off
set i=0
set group=%1
set user=%username%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group%
exit /B 0

:member
echo %user% is member of %group%
exit /B 1

Then run isMember.bat, your group name should be the parameter. You can then use the return code of this script in another one (variable ERROR_LEVEL). Make sure you run it with “call isMember.bat” command, without “call” your main script would also be exited.

Please note the “net user” command used above might cut long group names, so solution 1 could be a better way.

3)

Other ways, but also have to be parsed:

gpresult /user myAccount /r

 

whoami /groups

 

for /f "tokens=1 delims=," %g in ('whoami /groups /fo csv /nh') do @echo "%~g"