Last active
April 21, 2020 15:40
-
-
Save codecoll/bb3010c0d76f9c8799cc9024eb2f4e28 to your computer and use it in GitHub Desktop.
Get idle seconds of computer with powershell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://stackoverflow.com/questions/15845508/get-idle-time-of-machine | |
Add-Type @' | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace PInvoke.Win32 { | |
public static class UserInput { | |
[DllImport("user32.dll", SetLastError=false)] | |
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); | |
[StructLayout(LayoutKind.Sequential)] | |
private struct LASTINPUTINFO { | |
public uint cbSize; | |
public int dwTime; | |
} | |
public static DateTime LastInput { | |
get { | |
DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount); | |
DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks); | |
return lastInput; | |
} | |
} | |
public static TimeSpan IdleTime { | |
get { | |
return DateTime.UtcNow.Subtract(LastInput); | |
} | |
} | |
public static int LastInputTicks { | |
get { | |
LASTINPUTINFO lii = new LASTINPUTINFO(); | |
lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)); | |
GetLastInputInfo(ref lii); | |
return lii.dwTime; | |
} | |
} | |
} | |
} | |
'@ | |
$Idle = [PInvoke.Win32.UserInput]::IdleTime | |
Write-Host ($Idle.TotalSeconds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment