The Powershell uptime command retrieves how long a Windows system has been running since its last boot, using the Win32_OperatingSystem WMI class to report system uptime in days, hours, minutes, and seconds. No additional tools are needed beyond a standard Windows PowerShell prompt.
What You'll Need
- A Windows operating system (Windows 7 or later, including Windows 10 and 11)
- Access to PowerShell (built-in, no installation required)
- Standard user privileges (no admin rights needed for basic uptime query)
Step-by-Step
- Open PowerShell: Press Win + S, type "PowerShell", and click "Windows PowerShell" or "Windows PowerShell (Admin)" from the results. For basic uptime, the standard user version suffices.
- In the PowerShell window, type or paste the following command exactly:
(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTimeand press Enter. This returns the exact date and time the system last started. - To calculate and display uptime in a readable format, use:
[datetime]::Now - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTimeand press Enter. The output will show a timespan like "3.12:05:23:10.4567890" meaning 3 days, 12 hours, 5 minutes, 23 seconds, and 10 milliseconds. - For a cleaner, formatted output without milliseconds, run:
$uptime = [datetime]::Now - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime; "{0}d {1}h {2}m {3}s" -f $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Secondsand press Enter. This prints uptime as, for example, "3d 12h 5m 23s". - To create a reusable function for future use, paste this block into PowerShell:
function Get-Uptime { $lastBoot = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime $uptime = [datetime]::Now - $lastBoot return "{0}d {1}h {2}m {3}s" -f $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds }Press Enter after the closing brace. Then simply typeGet-Uptimeand press Enter anytime to get formatted uptime. - To make the function persist across sessions, add it to your PowerShell profile. First, check if your profile exists by typing
Test-Path $PROFILE. If it returnsFalse, create it withNew-Item -Path $PROFILE -ItemType File -Force. Then edit it withnotepad $PROFILE, paste the function code above, save, and close Notepad. Restart PowerShell to load the function automatically.
Common Mistakes
- Using
Get-Datealone: This only returns the current time, not uptime. Always subtract the last boot time from the current time. - Confusing
Get-WmiObjectwithGet-CimInstance: WhileGet-WmiObjectworks in older PowerShell versions,Get-CimInstanceis the modern, preferred method and is required in PowerShell 7+ and newer Windows versions. - Forgetting to format the output: The raw subtraction returns a
[timespan]object. Without formatting, it may display as "3.12:05:23:10.4567890" which is less readable than "3d 12h 5m 23s". - Assuming admin rights are needed: Querying uptime via Win32_OperatingSystem does not require elevated privileges; standard users can run these commands successfully.
- Trying to use Unix-style
uptime: Theuptimecommand does not exist natively in Windows PowerShell. Attempting to run it will result in a "command not found" error.
Tips
- Use
Get-CimInstanceoverGet-WmiObjectfor better compatibility with modern systems and PowerShell 7+. - If you need uptime in seconds only for scripting, use:
[int]([datetime]::Now - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime).TotalSeconds. - To remotely check another computer’s uptime (if you have permissions), add the
-ComputerNameparameter:(Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName SERVER01).LastBootUpTime. - Combine with Select-Object for custom output: .
- For logging, append timestamps:
"{0} - System uptime: {1}d {1}h {2}m {3}s" -f (Get-Date), $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds | Out-File -FilePath uptime.log -Append.
Troubleshooting
- Command not recognized: If you see "The term 'Get-CimInstance' is not recognized," you are likely using Windows PowerShell 2.0 or an extremely outdated system. Update to PowerShell 5.1 (built into Windows 10/11) or install PowerShell 7+ from Microsoft’s official site. Alternatively, use
Get-WmiObject -ClassName Win32_OperatingSystemas a fallback. - Access denied or empty output: This rarely occurs for Win32_OperatingSystem queries. If it does, confirm you are not running in a heavily restricted sandbox or constrained language mode. Try launching PowerShell normally (not as admin) and ensure no group policy blocks WMI access.
- Incorrect time format: If the output shows strange numbers or negative values, verify the system clock is correct. An incorrect BIOS time or failed time synchronization can skew results. Check
Get-Dateandw32tm /query /status. - Function not saving: If your custom
Get-Uptimefunction disappears after closing PowerShell, you likely didn’t save it to your profile. Revisit the profile creation steps and ensurenotepad $PROFILEopens a file you can edit and save. - Remote query fails: When using
-ComputerName, ensure:- The target computer is online and reachable (test with
ping SERVER01). - You have permission to query WMI on the target (domain admin or explicit DCOM/WMI permissions).
- Firewall rules allow WMI inbound traffic (ports 135 and dynamic RPC ports).
- Both computers are in the same domain or you’ve configured trusted hosts for Workgroup scenarios (
Set-Item WSMan:\localhost\Client\TrustedHosts -Value SERVER01).
- The target computer is online and reachable (test with
Frequently Asked Questions
Is there a native 'uptime' command in PowerShell like in Linux?
No, Windows PowerShell does not include a built-in uptime command. You must use WMI/CIM to query the LastBootUpTime property and calculate the difference from the current time.
Does the uptime command require administrator privileges?
No, querying the LastBootUpTime via Get-CimInstance -ClassName Win32_OperatingSystem works for standard users. No elevation is needed for this specific query on typical Windows systems.
Can I get uptime in just hours or minutes?
Yes. For total hours: [int]([datetime]::Now - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime).TotalHours. For total minutes: replace TotalHours with TotalMinutes.
Why does my uptime reset after sleep or hibernation?
Windows resets the LastBootUpTime only after a full shutdown or restart. Sleep and hibernation preserve the session, so uptime continues counting. However, a hybrid shutdown (fast startup) may appear to reset uptime less than a full cold boot.
Is this method accurate on virtual machines?
Yes. The Win32_OperatingSystem.LastBootUpTime property reflects when the guest OS last booted, regardless of whether it’s running on physical hardware or a hypervisor like Hyper-V, VMware, or VirtualBox.
Can I schedule this command to run automatically?
Absolutely. Save the uptime command or function to a .ps1 file and trigger it via Task Scheduler. For example, create a basic task that runs PowerShell.exe with the argument -Command "& {Get-Uptime}" daily at 9 AM to log system availability.