This week I wanted to do something a little bit fun. You’ve probably seen scripts that keep your mouse moving so your machine doesn’t go to sleep…
But have you ever seen one with PowerShell?
Yes, with the [System.Windows.Forms.Cursor]
type, you can move the mouse. Perfect!
Here’s a quick little script I put together to do just that:
Add-Type -AssemblyName System.Windows.Forms # Get max window size for primary monitor $WindowSize = [System.Windows.Forms.Screen]::GetWorkingArea(0) $MaxWidth = $WindowSize.Width $MaxHeight = $WindowSize.Height # Loop and update mouse position until you press Ctrl-C or close the console window while ($true) { $Pos = [System.Windows.Forms.Cursor]::Position $x = (Get-Random -Minimum 0 -Maximum $MaxWidth) $y = (Get-Random -Minimum 0 -Maximum $MaxHeight) Write-Host "`rNew position: x:$($x),y:$($y)" -NoNewLine [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y) Start-Sleep -Seconds 10 }
Now, who moved my cheese?