Calling GetLastInputInfo from PowerShell: Monitoring Idle Time from the Last Windows Input

PowerShellカテゴリを表すパンダのイラスト PowerShell

About This Article
This article was created using an automated generation workflow leveraging generative AI. It reviews the Microsoft Win32 API GetLastInputInfo specification and organizes a minimal test case using P/Invoke from PowerShell to observe the elapsed time since the last input.

Verification Status: 📘 Confirmed with official Microsoft documentation, unverified on a physical Windows device.

By calling Win32 GetLastInputInfo from PowerShell, you can observe how long there has been no keyboard or mouse input based on the timestamp of the last input event.

This test is successful if IdleSeconds can be retrieved as a numerical value.

Smoke Test

Add-Type @'
using System;
using System.Runtime.InteropServices;
public static class InputIdle {
  [StructLayout(LayoutKind.Sequential)]
  public struct LASTINPUTINFO { public uint cbSize; public uint dwTime; }
  [DllImport("user32.dll")]
  public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}
'@

$lii = New-Object InputIdle+LASTINPUTINFO
$lii.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($lii)
if ([InputIdle]::GetLastInputInfo([ref]$lii)) {
  $now32 = [uint32]([Environment]::TickCount64 -band 0xFFFFFFFFL)
  $idleMs = [uint32]($now32 - $lii.dwTime)
  "[SUCCESS] IdleSeconds = {0:N1}" -f ($idleMs / 1000)
} else {
  $code = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
  "[FAILED] GetLastInputInfo returned false. Win32Error=$code"
}

Observe

After checking the value, leave the mouse and keyboard untouched for a few seconds and run it again. Then, move the mouse and run it again to see if the value drops back down.

Why This Data Can Be Retrieved

Windows retains timestamp information for input events, and this API returns the last input information for the current session. This does not serve as proof of attendance or "the time the individual was actively working."

dwTimeSince

Try Changing One Thing

Change the display unit from seconds to minutes.

$idleMs / 60000

In the Daily-Code-Samples version, this can be toggled using -Unit Seconds / -Unit Minutes, allowing you to also check the original milliseconds, LastInputTick, and CurrentTick32.

Failures and Edge Cases

You need to be mindful of sessions, tick count handling, and type conversion. If the API returns false, do not treat it as a success and display the Win32 error as well. It is also important not to overinterpret the values for monitoring purposes.

If Used in Production

It can be used as an auxiliary check in personal local tools, such as "initiating heavy automated processing if there is no input for a certain period." Examples include auxiliary conditions before starting local processing of large CSV files, or experiments with display switching on demonstration terminals.

On the other hand, do not casually repurpose this for employee monitoring or attendance tracking. A lack of input is not synonymous with not working, and this API does not prove actual work activity.

Official Information

  • Microsoft Learn: GetLastInputInfo function

GitHub Sample

Document information

Article title
Calling GetLastInputInfo from PowerShell: Monitoring Idle Time from the Last Windows Input
Published
Updated
Source
https://papanda925.com/?p=16382&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL