Q. How can I convert a user's logon name (sAMAccountName) into their User Principal Name (userPrincipalName)?

I have scripted NTName2UPN.bat to convert a user's logon name (sAMAccountName) into their User Principal Name (userPrincipalName).

The syntax for using NTName2UPN.bat is:

\[Call\] NTName2UPN NTName NetBIOSDomain UPN


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

NetBIOSDomain is the NetBIOS domain name, like JSIINC

UPN           is a call directed environment variable that will contain the user's userPrincipalName.

If you wanted to return the current logged on user's UPN (User Principal Name)

call NTName2UPN %UserName% %USERDOMAIN% UPN
@echo %UPN%

NTName2UPN.bat contains:

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

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.