Get Domain Users Last Logon and Account Status Windows Batch

A typical output of net user command produces 3 columns of usernames with the last row containing up to three columns.

C:Users
kreider>net user
User accounts for \CTCRK-10
-------------------------------------------------------------------------------
Administrator            DefaultAccount           Guest
LocalAdmin
The command completed successfully.

To filter this, I’ll use a for loop and set 3 tokens. Tokens 2 and/or 3 could be empty when we get the last row, as you can see.

@echo off
setlocal
for /f "tokens=1-3" %%x in ('net users /domain^|find " " ') do (
 call :process %%x
 call :process %%y
 call :process %%z
)
pause
::-----
:process
if "%1"=="" goto :eof
REM echo User is %1
for /f "tokens=1-3" %%a in ('net user %1 /domain ^| findstr /r "active"') do (
 if "%%c"=="Yes" (
 for /f "tokens=1-5" %%e in ('net user %1 /domain ^| findstr /r "logon"') do (
 echo %1,ACTIVE,%%g %%h %%i
 )
 ) ELSE (
 echo %1,DISABLED
 )
)
:: other user processing here
goto :eof

Published by

Rich

Just another IT guy.

Leave a Reply

Your email address will not be published. Required fields are marked *