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

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 off
if \{%1\}

\{\} @echo Syntax SetUPN domain \[/R\]&exit /b 1 setlocal set Domain=%1 for /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 ) ) endlocal exit /b 0 goto :EOF :setupn set samid=%samid:"=% set samid=%samid: =.% set upn=%samid%@%Domain% if /i \{%2\}

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