Skip navigation

JSI Tip 5531. How can I create a file containing all the user names in my domain?

If you open a CMD prompt on your Workstation and type net user /domain, you will receive a display similar to:

The request will be processed at a domain controller for domain JSIINC.COM.

User accounts for \\JSI001.JSIINC.COM

Administrator            Guest                    IUSR_JSI001
IWAM_JSI001              UserName1                UserName2
UserName3                UserName4                UserName5
UserName6                UserName7
The command completed successfully.
I have scripted AllUsers.bat to parse this output and write the user names, one name per line, to a file name that you specify. The syntax is:

call AllUsers "FileName"

where FileName is the full path to the file that will contain the user names.

AllUsers.bat contains:

@echo off
if \{%1\}

\{\} @echo Syntax: AllUsers "FileName"&goto :EOF setlocal if exist %1 del /q %1 set filename=%1 for /f "Skip=4 Tokens=*" %%i in ('net user /domain^|findstr /v /c:"----"^|findstr /v /i /c:"The command completed"') do ( set line=%%i call :parse ) endlocal 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 goto :EOF :strip set short=%name%# set short=%short: =% set short=%short: #=#% set short=%short:#=% @echo %short%>>%filename%

Sample Usage

To create of report of all user names whose password has expired, or will expire today, Expired.bat contains:

@echo off
if \{%1\}\{\} @echo syntax: Expired "ReportFile"&goto :EOF
set filename=%1
if exist %filename% del /q %filename%
::Format Today as YYYYMMDD where %date% contains DAY MM/DD/YYYY
set today=%date:~10,4%%date:~4,2%%date:~7,2%
call allusers "%TEMP%\AllUsers.txt"
for /f "Tokens=*" %%i in ('type "%TEMP%\AllUsers.txt"') do set user=%%i&call :report
if exist %filename% del /q %filename%
goto :EOF
call GetUserInfo "%user%"
if not defined $line1 goto :EOF
if /i "%$line6%" EQU "No" goto :EOF
if /i "%$line9%" EQU "Never" goto :EOF
::Set Password Expires to YYYYMMDD
set passexp=%$line9:~6,4%%$line9:~0,2%%$line9:~3,2%
If "%passexp%" GTR "%today%" goto :EOF
::Set ruser to 26 characters
set work=%user%                          #
set ruser=%work:~0,26%
::set Full Name to 40 characters
set work=%$line2%                                        #
set fullname=%work:~0,40%
@echo %ruser%%fullname%%passexp%>>%filename%

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.