C:\_G\WWW\~ELISANET\INFO\tscmd198.html
<https://www.salminet.fi/info/tscmd198.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Mon 28-Sep-2026 10:40:00

 
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.


198} Get how long the system has been up since the last boot?

Using PowerShell-aided cmd.exe script. Compare with item#136.
  @echo off & setlocal enableextensions
  call :UptimeFromPS uptime_
  echo %%uptime_%%=%uptime_%
  endlocal & goto :EOF

  :: The indentation is essential to avoid incorrect syntax   
  :: The time separators are escaped \
  :UptimeFromPS
  setlocal
  for /f "usebackq delims=" %%A in (`powershell -NoLogo -NoProfile -Command ^
    "$u=(Get-Date)-(Get-CimInstance Win32_OperatingSystem).LastBootUpTime;"
    "$u.ToString('d\.hh\:mm\:ss')"`) do (set "return_=%%A")
  endlocal & set "%1=%return_%" & goto :EOF

The output might be e.g.
  C:\_M>C:\_D\TEST\UPTIME.CMD
  %uptime_%=3.09:04:56

For more PowerShell-aided cmd.exe command-line solutions see #powershell.

Using ordinary cmd.exe commands gives the last boot time (but not directly the uptime):
 @echo off & setlocal enableextensions
 for /f "tokens=3,4" %%d in (
   'net statistics workstation^|find "Statistics since"') do (
   set lastboot=%%d %%e
  )
 echo/
 echo Last booted %lastboot%
 endlocal & goto :EOF
The output might be e.g.
  C:\_M>C:\_D\TEST\CMSFAQ.CMD
  Last booted 20.08.2026 09:37:45

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd198.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd198
C:\_E\ARCZIP\COPYTONE.CMD /~info
https://www.salminet.fi/info/tscmd198.html
file:///c:/_g/www/~elisanet/info/tscmd198.html