How to Prevent Idle Mode in PowerShell
-
Use the
SendKeys
Method to Prevent Idle Mode With PowerShell - Move Mouse to Prevent Idle Mode With PowerShell
PowerShell is a powerful tool that can automate various tasks in the system. The desktop lock or idle mode can be disturbing when you have important work going on the computer.
It would be much easier to prevent the system from going to idle mode without being in front of the computer. This tutorial will introduce multiple methods to prevent idle mode automatically with PowerShell.
Use the SendKeys
Method to Prevent Idle Mode With PowerShell
The SendKeys()
method sends keystrokes and combinations of keystrokes to the active application.
$WShell = New-Object -Com "Wscript.Shell"
while (1) { $WShell.SendKeys("a"); sleep 60 }
In the above script, the first command creates a Wscript.Shell
object, and the second command creates an infinite loop where the key a
is pressed every minute. As a result, it will prevent the system’s desktop lock or screen timeout.
You should use the key which does not have any action in the active application. Or, you can open the text editors like Notepad
to output the keys instead of overloading the Windows input buffer.
You can also run the script for a limited time rather than the infinite period. The following script sends the key .
every minute for 1 hour to prevent the desktop lock screen.
param($minutes = 60)
$myshell = New-Object -com "Wscript.Shell"
for ($i = 0; $i -lt $minutes; $i++) {
Start-Sleep -Seconds 60
$myshell.sendkeys(".")
}
Move Mouse to Prevent Idle Mode With PowerShell
We all know that the mouse cursor’s movement stops the system from going to the idle or sleep mode. We will use PowerShell to automate the mouse movement every n
seconds.
The following script moves the mouse cursor every 5 seconds from its current location.
Add-Type -AssemblyName System.Windows.Forms
while ($true) {
$Pos = [System.Windows.Forms.Cursor]::Position
$x = ($pos.X) + 1
$y = ($pos.Y) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds 5
}