Skip to content

Instantly share code, notes, and snippets.

@Chinoman10
Last active April 17, 2025 00:57
Show Gist options
  • Select an option

  • Save Chinoman10/cd41ece8b69fc55e0f58854c6c47d434 to your computer and use it in GitHub Desktop.

Select an option

Save Chinoman10/cd41ece8b69fc55e0f58854c6c47d434 to your computer and use it in GitHub Desktop.
Prevent your computer from showing the screensaver/locking
<# This PowerShell script can simply be copied over to a "PowerShell IDE" window, and then ran for as long as you want.
The time this script remains active (sending mouse events to Windows so it things you're "still there", is the number
of iterations on line 6 (default: 200) multiplied by the number of seconds on line 11 (default: 290).
If your computer locks/marks as Away on Skype/Teams only after 10 minutes for example, change 290 seconds to 590.
#>
<# If you get a "File cannot be loaded because running scripts is disabled on this system.", then type in the PowerShell ISE console:
powershell -ExecutionPolicy ByPass -File filename-here.ps1
#>
param($iterations = 200)
$MOUSEEVENTF_MOVED = 0x00000001 ;
for ($i = 0; $i -lt $iterations; $i++) {
Start-Sleep -seconds 290
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($Pos.X , $Pos.Y)
$signature=@'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -PassThru
$SendMouseClick::mouse_event($MOUSEEVENTF_MOVED, 0, 0, 0, 0);
}
<# For the curious people, here are the other mouse event ID's, in case you want to actually move the mouse few pixels to the left/right, etc.
$MOUSEEVENTF_LEFTDOWN = 0x00000002 ;
$MOUSEEVENTF_LEFTUP = 0x00000004 ;
$MOUSEEVENTF_RIGHTDOWN = 0x00000008 ;
$MOUSEEVENTF_RIGHTUP = 0x00000010 ;
#>
@Chinoman10
Copy link
Copy Markdown
Author

Note: If your company policy forces your computer to lock after a few minutes of being away, there's a security reason behind it.
Make sure that you're only running the script on controlled environments like being alone at your apartment, and not somewhere public like a cafe, the office, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment