Time since Windows 7 was last unlocked

Is it possible to determine the time since a Windows 7 machine was last unlocked. By unlocked I mean the user locks the machine by clicking Start->ShutDown->Lock or by pressing the Windows key + L, and then at a later time unlocks the machine by clicking on the user icon (and depending on settings typing in password)?


Open Event Viewer, browse to Windows Logs > Security and look for an event with ID 4624:

Details for event 4624

Logon Type: 7 indicates a workstation unlock.


To show only Workstation Unlock events (i.e. Logon Type: 7):

  1. Open Event Viewer.
  2. In the right pane, click "Create Custom View".
  3. Click the "XML" tab.
  4. Check "Edit query manually".
  5. Click "Yes" at the confirmation box.
  6. Paste in this XML:

    <QueryList>
      <Query Id="0" Path="Security">
        <Select Path="Security">
          *[
            EventData[Data[@Name='LogonType']='7']
            and
            System[(EventID='4624')]
            and
            System[TimeCreated[timediff(@SystemTime) &lt;= 604800000]] <!-- Show only events in the last seven days -->
          ] 
        </Select>
      </Query>
    </QueryList>
    
  7. Click OK.
  8. Enter a name and description, e.g. "Unlock times", "Times when a user unlocked the computer".
  9. Click OK.
  10. Now, whenever you want to know the time you unlocked the computer, open the "Custom Views" folder in the left pane and select "Unlock times" (or whatever you named the filter in Step 8).

Source: Filtering Security Logs by User and Logon Type

Screenshot of XML tab in Custom View Properties dialog of Event Viewer


Powershell 6 one-liner (run as administrator):

(get-winevent -FilterHashtable @{Logname='Security';ID=4624;'LogonType'='7'}  -MaxEvents 1).TimeCreated

Earlier versions of Powershell:

(Get-winevent -FilterHashtable @{logname='security'; id=4624; starttime=(get-date).date} | where {$_.properties[8].value -eq 7})[0].TimeCreated