Q. How can a batch or logon script return the current user's CN?

Using standard commands, I have scripted CN.bat to return the current user's CN.

The syntax for using CN.bat is:

\[call\] CN FullName

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

CN.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: CN.bat FullName&goto :EOF
if exist "%TEMP%\CN.VBS" goto setCN
@echo.Dim array>"%TEMP%\CN.VBS"
@echo.On Error Resume Next>>"%TEMP%\CN.VBS"
@echo.Set objSysInfo = CreateObject("ADSystemInfo")>>"%TEMP%\CN.VBS" 
@echo.Set objUser = GetObject("LDAP://" ^& objSysInfo.UserName)>>"%TEMP%\CN.VBS"
@echo.array = split(objSysInfo.UserName, ",")>>"%TEMP%\CN.VBS"
@echo.ln = Len(array(0)) - 3 >>"%TEMP%\CN.VBS"
@echo.ps = 4 >>"%TEMP%\CN.VBS"
@echo.Wscript.Echo Mid(array(0), ps, ln)>>"%TEMP%\CN.VBS"
for /f "Tokens=*" %%n in ('cscript //nologo "%TEMP%\CN.VBS"') do (
 set %1=%%n

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.