Skip to main content

How to Get the Uptime of a Remote Computer in Batch Script

Tracking the uptime of remote computers is vital for system monitoring and ensuring servers are rebooted regularly or diagnosing instability. A machine that has been running for months might need patches, while one that reboots constantly indicates a problem.

In this guide, we will explore ways to get the uptime of a remote computer using Batch scripting. We will rely primarily on the systeminfo command and WMI (Windows Management Instrumentation) via wmic.

Method 1: Using systeminfo

The simplest native tool is systeminfo. This command retrieves a wealth of system configuration data, including the "System Boot Time," from which uptime can be derived.

Retrieving the Boot Time

You can use systeminfo directed at a remote computer using the /s parameter.

systeminfo /s RemoteComputerName | find "System Boot Time"
  • /s RemoteComputerName: Targets the specified remote computer.
  • | find "System Boot Time": Filters the output so you only see the line you care about.

Output Example:

System Boot Time: 1/15/2026, 8:45:00 AM

Challenges with systeminfo

While systeminfo is easy, it has significant drawbacks for scripting:

  1. Format Dependency: The date format changes based on the computer's regional settings (e.g., MM/DD/YYYY vs DD/MM/YYYY).
  2. No Direct Uptime: It only tells you when the computer booted, not how long it has been running. Calculating the difference between the current time and boot time in pure Batch script is extremely complex and error-prone.
  3. Slow: systeminfo queries many WMI classes before returning results, making it sluggish, especially over a network.

A faster and more structured approach is querying the WMI class Win32_OperatingSystem for its LastBootUpTime property using the wmic command.

Querying the Boot Time

wmic /node:"RemoteComputerName" os get lastbootuptime

Output Example:

LastBootUpTime
20260115084500.500000-300

This output looks cryptic, but it represents YYYYMMDDHHMMSS.mmmmmm±UUU (Year, Month, Day, Hour, Minute, Second, Microseconds, and timezone offset).

Creating an Uptime Calculation Script

Because Batch math is limited, calculating elapsed time from this raw WMI string is still tricky. The most robust way to calculate actual uptime (Days, Hours, Minutes) in a Batch script is to use a hybrid script that leverages PowerShell or VBScript under the hood for the date math.

Here is a Batch script that uses a simplified PowerShell one-liner to calculate and display the remote uptime cleanly.

@echo off
setlocal

set "TARGET_PC=Server01"

echo Getting uptime for %TARGET_PC%...

:: Call PowerShell to do the heavy lifting of WMI query and date math
powershell -NoProfile -Command ^
"try {" ^
"$os = Get-WmiObject Win32_OperatingSystem -ComputerName '%TARGET_PC%' -ErrorAction Stop;" ^
"$uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime);" ^
"Write-Host ('{0} Days, {1} Hours, {2} Minutes' -f $uptime.Days, $uptime.Hours, $uptime.Minutes)" ^
"} catch {" ^
"Write-Host ('Failed to retrieve uptime: ' + $_.Exception.Message)" ^
"}"

echo.
pause

Why Use PowerShell Inside Batch?

Batch scripting is powerful for automating file tasks and basic commands, but date arithmetic is its weakness. Instead of writing 50 lines of complex string parsing and leap-year calculations in Batch, simply letting PowerShell handle the DateTime conversion is:

  1. Much shorter.
  2. Immune to regional date format issues.
  3. Highly reliable.

Dealing with Access Denied Errors

If you run the systeminfo or wmic commands and receive an "Access is Denied" error, the account executing the script does not have Administrative privileges on the target computer.

Wrong Way: Assuming credentials pass automatically across non-domain networks.

wmic /node:"192.168.1.50" os get lastbootuptime

(Fails if you are on a workgroup or using mismatched accounts).

Correct Way (Specifying Credentials): If you need to connect using different credentials, you can specify them. However, passing passwords in plain text is insecure. Use prompt switches where possible or secure credentials managers.

REM WMIC allows specifying user and prompting for password
wmic /node:"192.168.1.50" /user:"AdminAccount" os get lastbootuptime

Creating a Bulk Uptime Checker

You can modify the hybrid script technique to check multiple computers from a list.

Create servers.txt:

Server01
Server02
192.168.1.100

Create CheckUptimes.bat:

@echo off
setlocal EnableDelayedExpansion

set "LIST_FILE=servers.txt"

if not exist "%LIST_FILE%" (
echo The file %LIST_FILE% was not found.
pause
exit /b
)

echo ========================================
echo Server Uptime Report
echo ========================================

for /f "usebackq tokens=*" %%C in ("%LIST_FILE%") do (
set "CURRENT_PC=%%C"
<nul set /p "=Checking !CURRENT_PC!... "

REM Use PowerShell to query WMI and return a clean string
set "RESULT="
for /f "delims=" %%U in ('powershell -NoProfile -Command ^
"try {" ^
"$os = Get-WmiObject Win32_OperatingSystem -ComputerName '!CURRENT_PC!' -ErrorAction Stop;" ^
"$uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime);" ^
"'{0}d {1}h {2}m' -f $uptime.Days, $uptime.Hours, $uptime.Minutes" ^
"} catch {" ^
"'OFFLINE or Access Denied'" ^
"}" 2^>nul') do (
set "RESULT=%%U"
)

if defined RESULT (
echo !RESULT!
) else (
echo ERROR: No response from PowerShell
)
)

echo ========================================
pause
UseBackQ

The usebackq option in the FOR loop is required when your file name contains spaces or might be enclosed in quotes, ensuring Batch treats the string as a filename, not a literal string.

Conclusion

Retrieving the uptime of a remote computer using pure batch commands typically requires systeminfo or wmic. While these provide the raw boot time, calculating total elapsed uptime computationally requires tricky date math. For the most robust and readable scripts, combining Batch's simplicity in handling loops and files with a tiny sliver of PowerShell for the DateTime math provides the best of both worlds.