Q. How can I convert a user's logon name (sAMAccountName) into their distinguished name (distinguishedName)?

I have scripted NTName2DN.bat to convert a user's logon name (sAMAccountName) into their distinguished name (distinguishedName).

The syntax for using NTName2DN.bat is:

\[Call\] NTName2DN NTName NetBIOSDomain DN


NTName        is the user's logon name, like Jerry.

NetBIOSDomain is the NetBIOS domain name, like JSIINC

DN            is a call directed environment variable that will contain the user's distinguishedName.

If you wanted to return the current logged on user's distinguished name:

call NTName2DN %UserName% %USERDOMAIN% DN
@echo %DN%

NTName2DN.bat contains:

@echo off
if \{%3\}==\{\} @echo Syntax: NTName2DN NTName NetBIOSDomain DN&goto :EOF
set NTName=%2\%1
set NTName="%NTName:"=%"
if exist "%TEMP%\NTName2DN.VBS" goto getDN
@echo.Dim objArguments>"%TEMP%\NTName2DN.VBS"
@echo.Set objArguments = Wscript.Arguments>>"%TEMP%\NTName2DN.VBS"
@echo.Set objTrans = CreateObject("NameTranslate")>>"%TEMP%\NTName2DN.VBS"
@echo.objTrans.Init "3", "">>"%TEMP%\NTName2DN.VBS"
@echo.objTrans.Set "3", objArguments(0)>>"%TEMP%\NTName2DN.VBS"
@echo.Wscript.Echo objTrans.Get("1")>>"%TEMP%\NTName2DN.VBS"
for /f "Tokens=*" %%d in ('cscript //nologo "%TEMP%\NTName2DN.VBS" %NTName%') do (
 set DN="%%d"
endlocal&set %3=%DN%

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.