JSI Tip 8553. How can I rename a user's distinguished name, from the command-line, in my Windows Server 2003 domain?
Jerold Schulman
October 7, 2004
1 Min Read
Using the LDIFDE.EXE tool, and the DSGET Active Directory command-line tool, I have scripted RenameUser.bat to rename the distinguished name of a user on a Windows Server 2003 domain controller, in a Windows Server 2003 domain.
The syntax for using RenameUser.bat is:
RenameUser "CurrentDN" "NewDN"
Where:
"CurrentDN" is the user's current distinguished name."NewDN" is the user's new distinguished name.
Examples:
RenameUser "CN=TEST Rename,CN=Users,DC=JSIINC,DC=COM" "CN=Rename TEST,CN=Users,DC=JSIINC,DC=COM"
RenameUser "CN=Rename TEST,CN=Users,DC=JSIINC,DC=COM" "CN=Rename Test,OU=OU_TEST,DC=JSIINC,DC=COM"
RenameUser.bat contains:
@echo offif {%2}=={} @echo Syntax: RenameUser "CurrentDN" "NewDN"&goto :EOFsetlocalset cDN=%1set nDN=%2set OK=Nfor /f "Tokens=*" %%a in ('dsget user %cDN% ^|findstr /I /C:"dsget succeeded"') do ( set OK=Y)If "%OK%" EQU "N" @echo RenameUser - %cDN% not found.&endlocal&goto :EOFfor /f "Tokens=*" %%a in ('dsget user %nDN% ^|findstr /I /C:"dsget succeeded"') do ( set OK=N)If "%OK%" EQU "N" @echo RenameUser - %nDN% already exists.&endlocal&goto :EOFset cDN=%cDN:"=%set nDN=%nDN:"=%(@echo dn: %cDN%@echo changeType: modrdn@echo newRdn: %nDN%@echo deleteOldRdn:1) >"%TEMP%RenameUser.ldf"set OK=Nfor /f "Tokens=*" %%a in ('ldifde -I -f "%TEMP%RenameUser.ldf" ^|findstr /I /C:"The command has completed successfully"') do ( set OK=Y)if "%OK%" EQU "N" @echo RenameUser "%cDN%" "%nDN%" failed.del /q "%TEMP%RenameUser.ldf"endlocal
About the Author
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