Q. How can my batch retrieve the Common Name of the current user?

Every object in Active Directory has a unique Distinguished Name, as in CN=Jerold Schulman,CN=Users,DC=JSIINC,DC=COM, where the first CN= defines the Common Name, Jerold Schulman.

Using standard commands, I have scripted UserCN.bat to retrieve the Common Name of the current user.

The syntax for using UserCN.bat is:

\[call\] UserCN CN

Where CN is a call directed environment variable that will contain the user's Common Name.

UserCN.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: UserCN CN&goto :EOF
if exist "%TEMP%\UserCN.vbs" goto doit
@echo.Set objSysInfo = CreateObject("ADSystemInfo") >"%TEMP%\UserCN.vbs"
@echo.strUserDN = objSysInfo.UserName>>"%TEMP%\UserCN.vbs"
@echo.Set objUser = GetObject("LDAP://" ^& strUserDN)>>"%TEMP%\UserCN.vbs"
@echo.Wscript.Echo objUser.cn>>"%TEMP%\UserCN.vbs"
set %1=Not Found
for /f "Tokens=*" %%a in ('cscript //nologo "%TEMP%\UserCN.vbs"') do (
 set %1=%%a

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.