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

Jerold Schulman

August 17, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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

Where:

NTName        is the user's logon name, like Jerry.NetBIOSDomain is the NetBIOS domain name, like JSIINCUPN           is a call directed environment variable that will contain the user's userPrincipalName.

Example:

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 offif {%3}=={} @echo Syntax: NTName2UPN NTName NetBIOSDomain UPN&goto :EOFsetlocalset NTName=%2%1set NTName="%NTName:"=%"if exist "%TEMP%NTName2UPN.VBS" goto [email protected] 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":getUPNset UPN="NOTFOUND"for /f "Tokens=*" %%d in ('cscript //nologo "%TEMP%NTName2UPN.VBS" %NTName%') do ( set UPN="%%d")endlocal&set %3=%UPN%


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like