Skip navigation

JSI Tip 7964. How can I retrieve all the user names in my domain in a script?

I have scripted GetUsers.bat to return all the user names in your domain in a script.

The syntax for using GetUsers.bat is:

\[call\] GetUsers

The output is displayed on the console, so you could pipe it to a file, or use it for subsequent processing, as in the following trivial examples:

:: Display all user names in alphabetical order
for /f "Tokens=*" %%u in ('getusers') do (
 @echo %%u
)


:: Display all user names that contain a Z, in any position, in any case, in alphabetical order
for /f "Tokens=*" %%u in ('getusers^|findstr /i /c:"Z"') do (
 @echo %%u
)


:: Display all user names with their Distinguished Name, using GetDN
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "Tokens=*" %%u in ('getusers') do (
 call GetDN "%%u" DN
 @echo "%%u" !DN!
)
endlocal

GetUsers.bat contains:
@echo off
setlocal
for /f "Skip=2 Tokens=*" %%i in ('net user /domain^|findstr /v /c:"User accounts for "^|findstr /v /c:"----"^|findstr /v /i /c:"The command completed"') do (
 set line=%%i
 call :parse
)
endlocal
goto :EOF
:strip
set short=%name%#
set short=%short:  =%
set short=%short: #=#%
set short=%short:#=%
@echo %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