How to Get System Uptime in Batch Script
Knowing how long a system has been running since its last reboot is a key piece of diagnostic information. It can help you determine if a server has rebooted unexpectedly, verify that a maintenance reboot was successful, or simply check the stability of a workstation. While there is no simple %UPTIME% variable, Windows provides several built-in command-line tools that can provide this information.
This guide will teach you the two most effective methods for retrieving the system uptime. We'll cover the modern approach using the systeminfo command, which is easy to use, and a more advanced (but faster) method using WMIC, both of which are easily integrated into a batch script.
The Modern Method (Recommended): Using systeminfo
The systeminfo command is a comprehensive utility that displays a wide range of system information. One of its default fields is "System Boot Time." By finding this line, we can determine the uptime.
We can pipe the output of systeminfo to the find command to isolate the line we need: systeminfo | find "System Boot Time"
The output is human-readable and very clear.
System Boot Time: 10/25/2023, 8:30:00 AM
This method is recommended for its simplicity and readability.
The Alternative Method (Faster): Using WMIC
The WMIC (Windows Management Instrumentation Command-line) utility can query the operating system's boot time directly. This method is often significantly faster than systeminfo because it requests only one specific piece of data instead of generating a full system report.
Command:WMIC OS GET LastBootUpTime
The output is a standardized, non-friendly timestamp format (YYYYMMDDHHMMSS.fractionalseconds).
LastBootUpTime
20231025083000.500000-300
While harder to read for a human, this format can be easier for a script to parse if you need to perform calculations with the date.
Basic Example: Displaying the Uptime
This script demonstrates how to call both commands to show the boot time.
@ECHO OFF
ECHO --- Getting System Uptime ---
ECHO.
ECHO Method 1: Using systeminfo (human-readable)
systeminfo | find "System Boot Time"
ECHO.
ECHO Method 2: Using WMIC (script-friendly)
WMIC OS GET LastBootUpTime
How to Capture the Uptime in a Variable
To use this information in a script, you need to capture the output into a variable. A FOR /F loop is the standard tool for this.
Script using systeminfo
@ECHO OFF
SET "BootTime="
REM 'tokens=2*' splits the line by the first space.
REM 'delims=:' is not used here, we parse the whole line.
FOR /F "tokens=1,*" %%A IN ('systeminfo ^| find "System Boot Time"') DO (
SET "BootTime=%%B"
)
REM The result will have some leading spaces, which we can remove.
FOR /F "tokens=*" %%T IN ("%BootTime%") DO SET "BootTime=%%T"
ECHO The system was last booted on: %BootTime%
Script using WMIC
This is simpler to parse because the output is more predictable.
@ECHO OFF
SET "BootTime="
REM 'skip=1' ignores the "LastBootUpTime" header.
FOR /F "skip=1" %%B IN ('WMIC OS GET LastBootUpTime') DO (
SET "BootTime=%%B"
GOTO :Done
)
:Done
ECHO The raw boot timestamp is: %BootTime%
How the methods work
systeminfomethod:systeminfogenerates a large text report. The pipe (|) sends this entire report to thefindcommand, which acts as a filter, only printing the single line that contains the string "System Boot Time". TheFOR /Floop then captures this filtered line.WMICmethod:WMICdirectly queries theWin32_OperatingSystemclass for theLastBootUpTimeproperty. This is a much more targeted and efficient query. TheFOR /F "skip=1"loop is used to discard the header line and capture the timestamp value.
Common Pitfalls and How to Solve Them
- Performance:
systeminfocan take several seconds to run because it gathers a lot of information.WMICis almost instantaneous. For scripts where speed matters,WMICis the better choice. - Language/Locale: The output of
systeminfois localized. On a German version of Windows, the string will be "Systemstartzeit," not "System Boot Time." This will break thefindcommand. The output ofWMICis not localized, making it far more robust for scripts that need to run on different machines. - Parsing Complexity: The human-readable
systeminfooutput is harder to parse if you need to extract just the date or time. TheWMICtimestamp is a fixed format (YYYYMMDDHHMMSS), making it very easy to parse with substring operations (e.g.,%BootTime:~0,4%for the year).
For these reasons, while systeminfo is easier for a human to read, WMIC is almost always the superior choice for scripting.
Practical Example: A Remote Server Uptime Check
This script uses WMIC's ability to query remote computers to check the last boot time of a server across the network.
@ECHO OFF
SETLOCAL
SET "TARGET_SERVER=DB-SERVER-01"
ECHO --- Checking Uptime for Remote Server ---
ECHO Target: %TARGET_SERVER%
ECHO.
SET "RemoteBootTime="
FOR /F "skip=1" %%B IN (
'WMIC /NODE:"%TARGET_SERVER%" OS GET LastBootUpTime'
) DO (
SET "RemoteBootTime=%%B"
GOTO :Done
)
:Done
IF NOT DEFINED RemoteBootTime (
ECHO [FAILURE] Could not retrieve uptime. Check server name and network connection.
) ELSE (
ECHO [SUCCESS] Raw boot timestamp for %TARGET_SERVER% is: %RemoteBootTime%
)
ENDLOCAL
Conclusion
Knowing the system uptime is a key diagnostic metric, and Windows provides two effective command-line tools to retrieve it.
- The
systeminfo | find "System Boot Time"method is simple, human-readable, and good for quick interactive checks. However, it is slow and not reliable across different language versions of Windows. - The
WMIC OS GET LastBootUpTimemethod is the recommended best practice for scripting. It is significantly faster, provides a standardized and easy-to-parse output, and works reliably across all language versions of Windows.