JSI Tip 7685. How can I set all user's UPNs in a script?

Jerold Schulman

January 22, 2004

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

Using the Active Directory command-line tools, in a Windows 2000 domain, or Windows Server 2003 domain, I have scripted SetUPN.bat to set all user UPNs.

The syntax for using SetUPN.bat is:

SetUPN domain [/R]

Where domain is the domain name to use, as in JSIINC.COM, and /R is an optional switch which causes all UPNs to be reset. When /R is omitted, only accounts without a configured UPN are updated.

SetUPN.bat contains:

@echo offif {%1}

{} @echo Syntax SetUPN domain [/R]&exit /b 1setlocalset Domain=%1for /f "Tokens=*" %%d in ('dsquery user -o dn -name * -limit 0') do ( for /f "Tokens=*" %%s in ('dsquery user -o samid %%d') do ( set samid=%%s set dn=%%d call :setupn ))endlocalexit /b 0goto :EOF:setupnset samid=%samid:"=%set samid=%samid: =.%set upn=%samid%@%Domain%if /i {%2}

{/r} goto setitfor /f "Tokens=*" %%u in ('dsquery user -o upn %dn%') do set oldupn=%%uif not %oldupn% EQU "" goto :EOF:setitdsmod user %dn% -upn %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