DSMOD script to set Users with blank UPN to same as email address

We are trying to write a script to locate all our user accounts that do not have the UPN field populated.  Then we need to add in the UPN to be the same as the Email address of the user.  So far I have been unable to locate how to script this. Any help or input is appreciated.    
asburycollegeAsked:
Who is Participating?
 
Henrik JohanssonConnect With a Mentor Systems engineerCommented:
The previous script will do it if you don't have any user that UPN set, but is different to e-mail address
For example upn=jdoe@domain.com will be overidden if email=john.doe@domain.com
Modified script below to exclude those that already has UPN set to xxx@*.
To only run it on a sub-OU change the first dsquery to be
dsquery user -limit 0 "OU=OU-path,DC=domain,DC=com"
for /F "tokens=1" %%a in ('dsquery user -limit 0 ^|dsget user -samid -upn^|find /v "@"') do (
  for /F "tokens=1,2" %%u in ('dsquery user -samid %%a ^|dsget user -samid -email^|find "@"') do (  
    dsquery user -samid %%a|dsmod user -upn %%v
  )
)

Open in new window

0
 
Henrik JohanssonSystems engineerCommented:
Copy code snippet into notepad and save file as *.cmd or *.bat
The script below will set UPN for all users in the domain to be e-mail address if having e-mail address set.
for /F "tokens=1,2" %%a in ('dsquery user -limit 0 ^|dsget user -samid -email^|find "@"') do (
  dsquery user -samid %%a|dsmod user -upn %%b
)

Open in new window

0
 
asburycollegeAuthor Commented:
I forgot to clarify in the original question.  We only need to have this performed on users whose UPN is currently blank.  We have some legacy users that do not have this information and some of the new technology we are implement utilizes the UPN field for authentication.  Is there a way to ONLY get and change users with a blank UPN field?
0
 
Henrik JohanssonSystems engineerCommented:
The previous script will do it if you don't have any user that UPN set, but is different to e-mail address
For example upn=jdoe@domain.com will be overidden if email=john.doe@domain.com
Modified script below to exclude those that already has UPN set to xxx@*.
To only run it on a sub-OU change the first dsquery to be
dsquery user -limit 0 "OU=OU-path,DC=domain,DC=com"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.