This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
136} How do I find out when my PC was last booted?
@echo off & setlocal enableextensions
for /f "tokens=3,4" %%d in (
'net statistics workstation^|find "Statistics since"') do (
set lastboot=%%d %%e
)
echo Last booted %lastboot%
endlocal & goto :EOF
The output might be e.g.
C:\_D\TEST>cmdfaq
Last booted 11.04.2006 09:17
Alternatively, suggested by Phil Robyn
@echo off & setlocal enableextensions
for /f "tokens=1,2" %%d in (
'dir /ah /s C:\pagefile.sys^|find /i "pagefile.sys"') do (
set lastboot=%%d %%e)
echo Last booted %lastboot%
endlocal & goto :EOF
There is yet another way. As it happens, I have the following script
in the Startup folder, which is directly pertinent to the question
discussed:
@echo off & setlocal enableextensions
set target_=C:\_L\TIMO\logon.txt
::
color 0B
echo +-------------------------------+
echo : Logging the user's logon :
echo : A script by Prof. Timo Salmi :
echo : Last modified Thu 15-Feb-2007 :
echo +-------------------------------+
echo.
echo %date% %time%
echo Making an entry to %target_%
::
if not defined temp goto :EOF
set temp_=%temp%
if exist c:\_l\temp set temp_=C:\_L\TEMP
for /f "tokens=*" %%f in ("%temp_%") do set temp_=%%~sf
::
gawk 'BEGIN{printf"@set wd_=%%s\n",strftime("%%a",systime())}'>%temp_%\weekday$.cmd
for %%c in (call del) do %%c %temp_%\weekday$.cmd
::
echo Logon: %COMPUTERNAME% %wd_% %date% %time% %USERNAME%>%temp_%\logon1.tmp
if not exist %target_% (
copy /y %temp_%\logon1.tmp %target_% > nul
goto _out
)
copy %target_% %temp_%\logon2.tmp > nul
copy /y %temp_%\logon1.tmp + %temp_%\logon2.tmp %target_% > nul
::
:_out
for %%f in (logon1.tmp logon2.tmp weekday$.cmd) do if exist %temp_%\%%f del %temp_%\%%f
endlocal & goto :EOF
Alternatively:
@echo off & setlocal enableextensions
ver|find "Microsoft Windows XP">nul
if %errorlevel% NEQ 0 (
echo Exiting: not Windows XP
goto :EOF)
for /f "tokens=* skip=1" %%i in ('wmic OS Get LastBootUpTime') do (
set LastBootUpTime=%%i)
echo LastBootUpTime: %LastBootUpTime%
endlocal & goto :EOF
systeminfo | find /i "System Boot Time:"
To get the System Up Time:
@echo off & setlocal enableextensions
ver|find "Microsoft Windows XP">nul
if %errorlevel% NEQ 0 (
echo Exiting: not Windows XP
goto :EOF)
for /f "tokens=2 delims=:" %%a in ('
systeminfo^|find "System Up Time:"') do (
set uptime_=%%a)
for /f "tokens=* delims= " %%a in ('
echo %uptime_%') do set uptime_=%%a
echo uptime_=%uptime_%
endlocal & goto :EOF
The output could be e.g.
C:\_D\TEST>cmdfaq
uptime_=2 Days 10 Hours 28 Minutes 46 Seconds