Listing 1: GetUserProfiles.bat @Echo Off Setlocal EnableDelayedExpansion :: BEGIN COMMENT :: Configure the location of the input file, NetUsers tool, and :: output file (spaces OK). :: END COMMENT Set PCList=D:\GetUserInfo2\nodes 3.txt Set NUToolLoc=D:\GetUserInfo2\NetUsers.exe Set OutputLoc=D:\GetUserInfo2\output.tsv :: BEGIN COMMENT :: No configuration is needed past this point, unless you want to use :: a .csv output file instead of a .tsv output file. :: END COMMENT :: BEGIN COMMENT :: Remove quotes, check for the files' existence, and convert paths to 8.3 format. :: END COMMENT Set token=5 For /F "tokens=*" %%i in ('ver^|Find "2000"') Do Set token=4 :: BEGIN CALLOUT A Set PCList="%PCList:"=%" :: END CALLOUT A :: BEGIN CALLOUT B If Not Exist %PCList% Echo PC List is missing & Pause & Exit /B Set NUToolLoc="%NUToolLoc:"=%" If Not Exist %NUToolLoc% Echo NetUsers Tool is missing & Pause & Exit /B Set OutputLoc="%OutputLoc:"=%" :: END CALLOUT B :: BEGIN COMMENT :: If you want to use a .csv output file, replace the tabs with commas. :: END COMMENT :: BEGIN CALLOUT C Echo Node?UserID?Friendly Name?Date?Time>%OutputLoc% If Not Exist %OutputLoc% Echo Output folder location is missing or not writeable & Pause & Exit /B :: END CALLOUT C :: BEGIN CALLOUT D For /f "tokens=%token%" %%i in ('Dir /X %PCList%^|Find "/"') Do Set SFNPCL=%%i For /f "tokens=%token%" %%i in ('Dir /X %NUToolLoc%^|Find "/"') Do Set SFNNTL=%%i For /f "tokens=%token%" %%i in ('Dir /X %OutputLoc%^|Find "/"') Do Set SFNOL=%%i Call :getshort %PCList% %NUToolLoc% %OutputLoc% :: END CALLOUT D Echo parsing input file: %PCList% :: BEGIN COMMENT :: Parse the input list for PC names. :: END COMMENT For /f "tokens=*" %%i in (%PCListVar%) Do (Set PC=%%i) & (Set flag=0) & (Call :pingit) Echo. Echo Run completed. Check output file: %OutputLoc% Pause Goto :EOF :: BEGIN COMMENT :: Check whether the target PC is on. :: END COMMENT :pingit Echo.>>%OutVar% Echo. For /F "tokens=1" %%i in ('Ping -n 1 %PC%^|Find "Reply"') Do Goto :getusers :: BEGIN COMMENT :: If you want to use a .csv output file, replace the tabs with commas. :: END COMMENT Echo %PC%?OFFLINE >>%OutVar% Echo %PC%?OFFLINE Goto :EOF :: BEGIN COMMENT :: Run NetUsers against the target PC. :: END COMMENT :getusers Echo querying PC: %PC% Set Datestr=N/A Set Friendstr=N/A :: BEGIN COMMENT :: Run NetUsers, filtering out the unwanted lines. Use the Sort command :: to sort the user IDs by Domain/UserID. Retrieve the user ID, and :: capture the rest of the string in the Remainder variable. :: END COMMENT For /f "tokens=1,*" %%i in ('%NUToolVar% \\%PC% /h ^|Find /V "---"^| Find /V "successfully."^|Find /V "History"^|Find /V "Connecting"^|Sort') :: BEGIN CALLOUT E Do (Set UserID=%%i) & (Set Remainder=%%j) & (Call :echoit) :: END CALLOUT E Goto :EOF :: BEGIN COMMENT :: Use string manipulations to extract the date and time from the :: Remainder variable and retrieve the friendly name. :: END COMMENT :echoit :: BEGIN COMMENT :: BEGIN CALLOUT F :: Extract date and time. :: END COMMENT Set Datestr=%Remainder:~-16,-6% Set Timestr=%Remainder:~-5% :: END CALLOUT F :: BEGIN COMMENT :: BEGIN CALLOUT G :: Remove the date from the Remainder variable. Set it to the Friendstr variable. :: END COMMENT Set Friendstr=!Remainder:%Datestr%=! :: BEGIN COMMENT :: Remove the time from the Friendstr variable. Reset it to the Friendstr variable. :: END COMMENT Set Friendstr=!Friendstr:%Timestr%=! :: BEGIN COMMENT :: Remove any double spaces from the Friendstr variable. :: END COMMENT Set Friendstr=!Friendstr: =! :: END CALLOUT G :: BEGIN COMMENT :: If this is the first loop, print the PC name. If this is a subsequent loop, :: don't print the PC name. If you want to use a .csv output file, :: replace the tabs with commas. :: END COMMENT If %flag%==1 Echo?%UserID%?%Friendstr%?%Datestr%?%Timestr% >>%OutVar% If %flag%==0 Echo %PC%?%UserID%?%Friendstr%?%Datestr%? %Timestr% >>%OutVar% If %flag%==1 Echo?%UserID%?%Friendstr%?%Datestr%?%Timestr% If %flag%==0 Echo %PC%?%UserID%?%Friendstr%?%Datestr%?%Timestr% :: BEGIN COMMENT :: Reset flag variable to signal this is the first time through the loop. :: END COMMENT Set flag=1 Goto :EOF :: BEGIN CALLOUT H :getshort Set PCListVar=%~dp1 Set NUToolVar=%~dp2 Set OutVar=%~dp3 Call :getshort2 "%PCListVar%%SFNPCL%" "%NUToolVar%%SFNNTL%" "%OutVar%%SFNOL%" Goto :EOF :: END CALLOUT H :getshort2 Set PCListVar=%~fs1 Set NUToolVar=%~fs2 Set OutVar=%~fs3 Goto :EOF