Q. How can I set a variable to the current user's logon date and time?

Jerold Schulman

November 9, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

If you don't need to know the logon time of user, even if they are logged off, but only need the logon date and time of the current logged on user, use LogonTime.bat.

The syntax for using LogonTime.bat is:

[call] LogonTime LT

Where LT is a call directed environment variable that will contain the date and time of the current user's logon, like MM/DD/YYYY HH:MM:SS.

NOTE: LogonTime.bat uses LogonSessions.exe, which should be located in a folder that is in your PATH, or in the same folder as LogonTime.bat.

LogonTime.bat contains:

@echo offif {%1}=={} @echo Syntax: LogonTime LT&goto :EOFsetlocalset sess=Ncall :quiet>nul 2>&1if defined lt endlocal&set %1=%lt%&goto :EOFendlocal&set %1=0goto :EOF:quietfor /f "Tokens=*" %%a in ('logonsessions') do ( set line=%%a call :parse)goto :EOF:parseset wrk=%line:Logon session=%if "%wrk%" NEQ "%line%" set sess=Y&goto :EOFset wrk=%line:User name:    =%if "%wrk%" NEQ "%line%" set user=%wrk%&goto :EOFset wrk=%line:Logon type:   =%if "%wrk%" NEQ "%line%" goto isIntset wrk=%line:Logon time:   =%if "%wrk%" NEQ "%line%" goto ltgoto :EOF:isIntif "%wrk%" EQU "Interactive" goto :EOFset sess=Ngoto :EOF:ltif "%sess%" EQU "N" goto :EOFset lt=%wrk%


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like