<p>The need arose, in our product, to determine how long the current user has been logged on to Windows (specifically, Vista). It seems there is no straight forward API function for this and I couldn't find anything relevant with WMI (although I'm no expert with WMI, so I might have missed something).</p>

<p>Any ideas?</p>

[{'answer_id': 13581, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 1, 'selected': False, 'text': '<p>In WMI do: "select * from Win32_Session"\nthere you\'ll have "StartTime" value.</p>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 13600, 'author': 'Nickolay', 'author_id': 1026, 'author_profile': 'https://Stackoverflow.com/users/1026', 'pm_score': 3, 'selected': True, 'text': '<p>For people not familiar with WMI (like me), here are some links:</p>\n\n<ul>\n<li>MSDN page on using WMI from various languages: <a href="http://msdn.microsoft.com/en-us/library/aa393964(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa393964(VS.85).aspx</a></li>\n<li>reference about Win32_Session: <a href="http://msdn.microsoft.com/en-us/library/aa394422(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394422(VS.85).aspx</a>, but the objects in Win32_session are of type Win32_LogonSession (<a href="http://msdn.microsoft.com/en-us/library/aa394189(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394189(VS.85).aspx</a>), which has more interesting properties.</li>\n<li><a href="http://www.ks-soft.net/hostmon.eng/wmi/index.htm" rel="nofollow noreferrer">WMI Explorer</a> - a tool you can use to easily run queries like the one Michal posted.</li>\n</ul>\n\n<p>And here\'s example querying Win32_Session from VBS:</p>\n\n<pre><code>strComputer = "."\nSet objWMIService = GetObject("winmgmts:" _\n    &amp; "{impersonationLevel=impersonate}!\\\\" _\n    &amp; strComputer &amp; "\\root\\cimv2")\nSet sessions = objWMIService.ExecQuery _\n   ("select * from Win32_Session")\n\nFor Each objSession in sessions\n   Wscript.Echo objSession.StartTime\nNext\n</code></pre>\n\n<p>It alerts 6 sessions for my personal computer, perhaps you can filter by LogonType to only list the real ("interactive") users. I couldn\'t see how you can select the session of the "current user".</p>\n\n<p>[edit] and here\'s a result from Google to your problem: <a href="http://forum.sysinternals.com/forum_posts.asp?TID=3755" rel="nofollow noreferrer">http://forum.sysinternals.com/forum_posts.asp?TID=3755</a></p>\n'}, {'answer_id': 543364, 'author': 'Matt Hanson', 'author_id': 5473, 'author_profile': 'https://Stackoverflow.com/users/5473', 'pm_score': 0, 'selected': False, 'text': '<p>Using WMI, the Win32Session is a great start. As well, it should be pointed out that if you\'re on a network you can use Win32_NetworkLoginProfile to get all sorts of info.</p>\n\n<pre><code>Set logins = objWMIService.ExecQuery _\n   ("select * from Win32_NetworkLoginProfile")\nFor Each objSession in logins\n   Wscript.Echo objSession.LastLogon\nNext\n</code></pre>\n\n<p>Other bits of info you can collect include the user name, last logoff, as well as various profile related stuff.</p>\n'}, {'answer_id': 43794118, 'author': 'Gnat', 'author_id': 478380, 'author_profile': 'https://Stackoverflow.com/users/478380', 'pm_score': 2, 'selected': False, 'text': '<p>In Powershell and WMI, the following one-line command will return a list of objects showing the user and the time they logged on.</p>\n\n<pre><code>Get-WmiObject win32_networkloginprofile | ? {$_.lastlogon -ne $null} | % {[PSCustomObject]@{User=$_.caption; LastLogon=[Management.ManagementDateTimeConverter]::ToDateTime($_.lastlogon)}}\n</code></pre>\n\n<p>Explanation:</p>\n\n<ul>\n<li>Retrieve the list of logged in users from WMI</li>\n<li>Filter out any non-interactive users (effectively removes <code>NT AUTHORITY\\SYSTEM</code>)</li>\n<li>Reformats the user and logon time for readability</li>\n</ul>\n\n<p>References:</p>\n\n<ul>\n<li>The WMI object to use: <a href="https://forum.sysinternals.com/topic3755.html" rel="nofollow noreferrer">https://forum.sysinternals.com/topic3755.html</a></li>\n<li>Formatting the date/time: <a href="https://blogs.msdn.microsoft.com/powershell/2009/08/12/get-systemuptime-and-working-with-the-wmi-date-format/" rel="nofollow noreferrer">https://blogs.msdn.microsoft.com/powershell/2009/08/12/get-systemuptime-and-working-with-the-wmi-date-format/</a></li>\n</ul>\n'}, {'answer_id': 64367682, 'author': 'mhmd', 'author_id': 12484958, 'author_profile': 'https://Stackoverflow.com/users/12484958', 'pm_score': 2, 'selected': False, 'text': '<p>You can simply use CMD or PowerShell to query the users using the command:</p>\n<pre><code>C:\\&gt; query user\nUSERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME\njohn                  rdp-tcp#56          9  Active          .  5/3/2020   10:19 AM\nmax                   rdp-tcp#5          30  Active    5+23:42  9/4/2020   7:31  PM\nyee                                      35  Disc         6:41  10/14/2020 6:37  PM\nmohammd               rdp-tcp#3          37  Active          .  10/15/2020 7:51  AM\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/13578', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1596/']