Skip navigation

JSI Tip 7708. What user accounts in my domain have no Department configured?

Using the Active Directory command-line tools, in a Windows 2000 domain, or Windows Server 2003 domain, I have scripted NoDepartment.bat to display all the distinguished names of domain users who have no Department configured.

The syntax for using NoDepartment.bat is:

NoDepartment

The output is displayed on the CMD console, but you can pipe it to a file using the following syntax:

NoDepartment>FileName

You can use the output in subsequent commands, as in:

for /f "Tokens=*" %%i in ('NoDepartment') do SomeCommand %%i

NoDepartment.bat contains:

@echo off
setlocal
if exist "%TEMP%\NoDepartment.TM1" del /q "%TEMP%\NoDepartment.TM1"
if exist "%TEMP%\NoDepartment.TM2" del /q "%TEMP%\NoDepartment.TM2"
set query=dsquery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr distinguishedName department -limit 0
for /f "Tokens=*" %%u in ('%query%') do set line=%%u&call :parse
if not exist "%TEMP%\NoDepartment.TM1" goto done
sort "%TEMP%\NoDepartment.TM1" /O "%TEMP%\NoDepartment.TM2"
type "%TEMP%\NoDepartment.TM2"
del /q "%TEMP%\NoDepartment.TM1"
del /q "%TEMP%\NoDepartment.TM2"
:done
endlocal
exit /b 0
:parse
if /i "%line:~0,17%" NEQ "distinguishedName" goto detail
set /a pos=17
:loop
set /a pos=%pos% + 1
call set work=%%line:~%pos%^,11%%
if /i "%work%" NEQ " Department" goto :loop
set /a pos=%pos% + 1
set /a len=%pos% - 2
goto :EOF
:detail
call set dpt=%%line:~%pos%%%
if "%dpt:~0,11%" NEQ "           " goto :EOF
call set dn="%%line:~0,%len%%%"
set dn=%dn:   =%
set dn=%dn:  =%
set dn=%dn: "="%
@echo %dn%>>"%TEMP%\NoDepartment.TM1"



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