JSI Tip 10047. How can I display all the local administrators on all the computers in my domain?

Using standard commands, I have scripted AllLocalAdmins.bat to output a line, in CSV format, for each local administrator on all the computers, in the domain that you are logged onto. When you run AllLocalAdmins in a CMD.EXE window, each line of output contains:

"Computer Name","User Account"

Here is a partial sample:

"JSI001","JSIINC\Enterprise Admins"
"JSI001","JSIINC\Domain Admins"
"JSI007","JSIINC\Domain Admins"
"JSI009","JSIINC\Domain Admins"
NOTE: AllLocalAdmins.bat uses Win32_PingStatus, so it must be run on Windows XP, or Windows Server 2003, or a later operating system.

AllLocalAdmins.bat contains:

@echo off
if exist "%TEMP%\AllLocalAdmins.VBS" goto doit
@echo.Dim WshShell, colGroup, oDomain, strComputer, Item>"%TEMP%\AllLocalAdmins.VBS"
@echo.Set WshShell = CreateObject("WScript.Shell")>>"%TEMP%\AllLocalAdmins.VBS"
@echo.Set oDomain = GetObject("WinNT://" ^& strDomain)>>"%TEMP%\AllLocalAdmins.VBS"
@echo.oDomain.Filter = Array("Computer")>>"%TEMP%\AllLocalAdmins.VBS"
@echo.Set objWMIService = GetObject("winmgmts:\{impersonationLevel=impersonate\}^!\\.\root\cimv2")>>"%TEMP%\AllLocalAdmins.VBS"
@echo.for each strComputer in oDomain>>"%TEMP%\AllLocalAdmins.VBS"
@echo.Set computers = objWMIService.ExecQuery ("Select * from Win32_PingStatus Where Address = '" ^& strComputer.Name ^& "'")>>"%TEMP%\AllLocalAdmins.VBS"
@echo.For Each objComputer in computers>>"%TEMP%\AllLocalAdmins.VBS"
@echo.     If objComputer.StatusCode = 0 Then>>"%TEMP%\AllLocalAdmins.VBS"
@echo.         Set colGroup = GetObject("WinNT://" ^& strComputer.Name ^& "/Administrators,group")>>"%TEMP%\AllLocalAdmins.VBS"
@echo.         For Each item In ColGroup.Members>>"%TEMP%\AllLocalAdmins.VBS"
@echo.         Wscript.Echo strComputer.Name ^& " " ^& item.ADsPath>>"%TEMP%\AllLocalAdmins.VBS"
@echo.         Next>>"%TEMP%\AllLocalAdmins.VBS"
@echo.     Else>>"%TEMP%\AllLocalAdmins.VBS"
@echo.         Wscript.Echo strComputer.Name ^& " NOT available.">>"%TEMP%\AllLocalAdmins.VBS"
@echo.    End If>>"%TEMP%\AllLocalAdmins.VBS"
for /f "Tokens=1*" %%a in ('cscript //nologo "%TEMP%\AllLocalAdmins.VBS"') do (
 set comp=%%a
 set wrk1=%%b
 set wrk2=!wrk1:~8!
 for /f "Tokens=1,2* Delims=/" %%x in ('@echo !wrk2!') do (
  set p1=%%x
  set p2=%%y
  set p3=%%z
 if "!p3!" NEQ "" set p1=!p2!&set p2=!p3!
 @echo "!comp!","!p1!\!p2!"

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.