System Uptime on Windows10

7 posts in this topic

I was looking for a way to find out how long my PC had been running a few weeks ago and had trouble finding something that satisfied my needs,  I found a Visual Basic Script that worked but found it too long. But searching trough AutoIt WINAPI help found that it really was as simple as the following little script.

#include <WinAPISys.au3>
#include <WinAPIMisc.au3>

Local $Uptime = _WinAPI_StrFromTimeInterval(_WinAPI_GetTickCount())
MsgBox(0, '', "PC Uptime ==> " & $UpTime)

Hope it helps...

2 people like this

Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

Welcome ces1a,

That is a very quick and useful way to get the uptime, yes.  I hadn't come across that method before; however, reading the verbiage on the _GetTick function from the help file, this method will be unreliable if the machine is "up" for longer than ~48+ days.  The help article also suggests using probing the system uptime counter from performance data...another way I've not done (or seen). 

Sure, the likely hood of a Windows machine being online that long is pretty low (if it is patched monthly), but it might be worth while thinking about these limitations and consider how you would address those limitations if necessary.

An alternative approach, which I have seen and used, is to collect the system boot time and calculate the duration from "now".  Here's an old(er) link to some examples. ;) https://www.autoitscript.com/forum/topic/110300-system-uptime/#comment-774837

I'm not trying to critique too harshly...I just noticed this is your first post and wanted to expand your knowledge in case you weren't aware or didn't think about it.  Once again welcome.


edit: to be fair...I have not tested those examples on Win 10 or verified they work with the latest ver(s) of AutoIt.  I just wanted to provide some more code for you to check out.

Edited by spudw2k

Share this post

Link to post
Share on other sites

You are right about the limitations of the method but if you modify it as follows it works.

#include <WinAPISys.au3>
#include <WinAPIMisc.au3>

Local $Uptime = _WinAPI_StrFromTimeInterval(_WinAPI_GetTickCount64())
MsgBox(0, '', "PC Uptime ==> " & $UpTime)


The only difference is the line: 

   Local $Uptime = _WinAPI_StrFromTimeInterval(_WinAPI_GetTickCount())

Is changed to:

    Local $Uptime = _WinAPI_StrFromTimeInterval(_WinAPI_GetTickCount64())

Thanks for your input.

Share this post

Link to post
Share on other sites

Good info here. Thanks!

Just a server analyst that has never been into programming that much. So, small fish here :)

Share this post

Link to post
Share on other sites

There is a really easy command for this.

In command prompt, type "net statistics workstation" and press enter. You will see "Statistics since <DatePCStarted> <TimePCStarted>" at the top which essentially tells you when you started your computer.


btw, you can just press windows key + r (run dialog) and type "cmd /k net statistics workstation" for the same effect. the /k flag tells cmd to remain open and accept new commands once the command is processed. Optionally, you can do "cmd /c pause | net statistics workstation" instead if you don't want cmd to accept new commands but still remain open. Note Pause must be first entry or it won't actually pause.

1 person likes this

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post

Link to post
Share on other sites

Thanks for the info, BetaLeaf, but what I needed in my case is to run a program when my computer starts.

The program has to wait 3 minutes from the time the laptop started to run two programs so as not to slow Windows startup in any way.

Getting the System Uptime had to take place automatically.  This way when the computer has been running close to three minutes my script runs two of my other programs.


Share this post

Link to post
Share on other sites

Put a 3 minute delay at the start of the script you're running, that way no matter when it starts running the rest of the script won't do anything until 3 minutes later.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question
      about the display language of the system
      How can I get the current display language and how can I change it by autoit
      Of course, if this is possible
      Greetings to all and hope you help me
    • DLS
      By DLS
      I have a working script that changes the core affinity and process priority of multiples of a specific application I have running.  
      I have that part figured out.

      I would like to make a little modification to it.

      Windows10 introduced virtual desktops.  I am trying to have different core affinity and priority of processes on the visible and non-visible desktops.  
      What I need is a bool function that could be described as IsWindowOnCurrentDesktop($hWnd).  I have searched the winAPI.au3 but I do not believe anything like that exists built it.
      I have searched multiple places before asking for help. I found a Microsoft supplied example of the function I need using C#, but I am unfamiliar with C# to a degree that I cannot port the system call over.
      Help is appreciated but not expected.  Thanks in advanced.   In the meantime I will be learning C# syntax and class structure.
    • Simpel
      By Simpel
      after I recognized that some of my scripts fail if they are at windows startup I researched for a solution. Problem could be fixed if I wait about 120 seconds after login. So how to measure this?
      #include-once #include <AD.au3> #include <Date.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SecondsAfterLogin ; Description ...: Returns how many seconds are gone since last login ; Syntax ........: _SecondsAfterLogin() ; Parameters ....: None ; Return values .: Success - Integer of seconds ; Failure - 0, sets @error to 1 ; Author ........: Conrad Zelck ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SecondsAfterLogin() Local $sDate Local $iSec $sDate = __GetLogonTime() ; UTC If @error Then $sDate = __GetLogonTime_AD() ; local time If @error Then Return SetError(1, 0, 0) Else $sDate = __LocalTime($sDate) ; UTC to local time EndIf $iSec = __TimeDifference($sDate) Return $iSec EndFunc #region - INTERNAL_USE_ONLY Func __GetLogonTime($sUserName = @UserName, $sComputerName = @ComputerName) ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/ Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0) If @error Or $aRet[0] Then Return SetError(1, 0, 0) Local $sSeconds = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 10) DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4]) Local $sLastLogon = _DateAdd('s', Number($sSeconds), "1970/01/01 00:00:00") Return $sLastLogon EndFunc Func __GetLogonTime_AD() Local $iSuccess = _AD_Open() If $iSuccess = 1 Then Local $sDate = _AD_GetLastLoginDate() $sDate = __NumberDate_StringDate($sDate) _AD_Close() Return $sDate Else Return SetError(1, 0, 0) EndIf EndFunc Func __NumberDate_StringDate($sDate) Local $y, $m, $d, $h, $min, $s $y = StringMid($sDate,1, 4) $m = StringMid($sDate,5, 2) $d = StringMid($sDate,7, 2) $h = StringMid($sDate,9, 2) $min = StringMid($sDate,11, 2) $s = StringMid($sDate,13, 2) $sDate = $y & "/" & $m & "/" & $d & " " & $h & ":" & $min & ":" & $s Return $sDate EndFunc Func __LocalTime($sDate) Local $aTimeZone = _Date_Time_GetTimeZoneInformation() If $aTimeZone[0] <> 2 Then $iTimeZoneOffset = ($aTimeZone[1]) * -1 Else $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1 EndIf $iTimeZoneOffset /= 60 ; in hours Local $sHour = StringMid($sDate, 12, 2) $sHour += $iTimeZoneOffset $sDate = StringLeft($sDate, 11) & $sHour & StringRight($sDate, 6) Return $sDate EndFunc Func __TimeDifference($sDate) Local $sNow = _NowCalc() Local $sDiff = _DateDiff("s", $sDate, $sNow) Local $iDiff = Number($sDiff) Return $iDiff EndFunc #endregion - INTERNAL_USE_ONLY I had to look what kind of login it is. Some of my computers are logging in with active directory and other not.
      Here is an example:
      #include <SecondsAfterLogin.au3> Local $iSec = _SecondsAfterLogin() MsgBox(0, '', "Time since last login: " & _HHMMSS($iSec, "s")) Exit Func _HHMMSS($iTime, $sType = "ms") If $sType = "s" Then Local $sHHMMSS = StringFormat("%.2d:%.2d:%.2d", (Floor($iTime / 3600)), (Floor(Mod($iTime,3600) / 60)), (Mod(Mod($iTime,3600),60))) Else Local $sHHMMSS = StringFormat("%.2d:%.2d:%.2d", (Floor($iTime / 3600000)), (Floor(Mod($iTime,3600000) / 60000)), (Mod(Mod($iTime,3600000),60000) / 1000)) EndIf Return $sHHMMSS EndFunc I hope someone find it useful.
      Regards, Conrad
      P.S. Review is welcome.
    • nss
      By nss
      Hi all,
      I'm making an app that among other things has the system info feature, where it displays stuff like the system version, build, CPU and ram stats, etc.
      So, I haven't been able to find anywhere a way to access the following things (is there even?):
      Windows version (not like WIN_10, but the version like 1607, or 1511 Windows edition like professional, home, education, etc. information whether the Windows is registered or not (optionally the registrant's name) the type of the currently logged-in user (e.g., administrator) computer manufacturer and model (e.g., dell inc. Inspiron n5110 boot mode (e.g., safe, normal) CPU tempreture I'd appreciate a lot if any of you could help me with any of the things from above; it would mean a lot to me.
      Have a great day.
    • computergroove
      By computergroove
      I am trying to start my script on Windows startup and it doesn't run. I have disabled defender and smartscreen with no luck. There is no antivirus on this machine. I have added the script to shell:startup, shell:common startup and HKLM\Software\Microsoft\Windows\Current Version\Run all with no luck. Im at a loss. Please help.