C:\_G\WWW\~ELISANET\INFO\tscmd136.html
<http://www.elisanet.fi/tsalmi/info/tscmd136.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Wed 7-Nov-2018 10:01:14

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

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


Windows 7
  systeminfo | find /i "System Boot Time:"

To get the System Up Time:
Windows 7
  @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

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Apr 16 2006, 8:50 am [M]
  How to find Windows uptime?
  A Description of the Windows Management Instrumentation (WMI) Command-Line Utility (Wmic.exe)
  Uptime.exe Tool Allows You to Estimate Server Availability with Windows NT 4.0 SP4 or Higher
  Easily Determine Windows Uptime in Windows 7, Vista or XP

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd136.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd136
http://www.elisanet.fi/tsalmi/info/tscmd136.html
file:///c:/_g/www/~elisanet/info/tscmd136.html