DEV Community

Sachin Varghese
Sachin Varghese

Posted on

Keep awake - ps script

Thanks for the image — the issue is clear!

❌ Error Explained:

Method invocation failed because [System.Object[]] does not contain a method named 'op_Addition'

This means you're trying to add to a value that’s not a point object, probably because .Position is returning an array or something not cast correctly.


✅ Fix — Cast the mouse position correctly before doing math

Update your script like this:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

Clear-Host
Write-Host "...Lock screen avoider with mouse movement..."

$sleep = 30 # seconds

while ($true) {
# Get and cast current mouse position properly
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y

# Move slightly and back [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x + 1, $y) Start-Sleep -Milliseconds 100 [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) Write-Host "Mouse moved to prevent lock. Waiting $sleep seconds..." Start-Sleep -Seconds $sleep 
Enter fullscreen mode Exit fullscreen mode

}


✅ Why this works:

$pos.X and $pos.Y safely extract coordinates as numbers.

New-Object System.Drawing.Point(...) constructs a valid point object for cursor movement.

Avoids the invalid attempt to do math on an object array.


Let me know if you'd like:

To run it in the background silently

A scheduled task version

A .ps1 file ready to download and use

Top comments (0)