JSI Tip 9362. How can I generate a CSV file of domain group membership?

In tip 9346, we generated a CSV file of local group membership.

Using the NET GROUP command, I have scripted NetGroup.bat to generate a CSV format output on the console, that contains:

"Computer Name","Group Name","Member Name"

NOTE: To redirect the output to a CSV file, use NetGroup>FileName.CSV.

NOTE: See What Active Directory groups exist that are not enumerated by the 'net group /domain' command?

NetGroup.bat contains:

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "Tokens=*" %%a in ('net group /domain^|findstr /b /l /c:"*"') do (
 set wrk=%%a
 set grp=!wrk:~1!
 for /f "Skip=8 Tokens=*" %%i in ('net group "!grp!" /domain^|find /V /I "The command completed successfully."') do (
  set line=%%i
  call :parse
 )
)
endlocal
goto :EOF
:strip
set short=%name%#
set short=%short:  =%
set short=%short: #=#%
set short=%short:#=%
@echo "%ComputerName%","!grp!","%short%"
goto :EOF
:parse
set name=%line:~0,25%
call :strip
set name=%line:~25,25%
if not "%name%" EQU "" call :strip
set name=%line:~50,25%
if not "%name%" EQU "" call :strip



Hide comments

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