JSI Tip 9284. How can I retrieve the local group membership for the current domain user?

I have scripted LocalGroupMembership.bat to retrieve up to 2 local groups that the logged on domain user is a member of. The script uses the Local Group Memberships line returned by the net user "%UserName%" /domain command.

The syntax for using LocalGroupMembership.bat is:

LocalGroupMembership Group1 Group2

Where Group1 and Group2 are call directed environment variables that will contain the NetBIOS group name, or NONE.

LocalGroupMembership.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: LocalGroupMembership Group1 Group2&goto :EOF
for /f "Tokens=*" %%a in ('net user "%UserName%" /domain^|Findstr /I /B /L /C:"Local Group Memberships      *"') do set line=%%a
if not defined line endlocal&goto :EOF
for /f "Tokens=2,3 Delims=*" %%a in ('@echo %line%') do (
 set lg1=%%a#
 set lg2=%%b
if defined lg1 set lg1=%lg1: #=%
if not defined lg1 set lg1=NONE
if not defined lg2 set lg2=NONE
endlocal&set %1=%lg1%&set %2=%lg2%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.