Sometimes you just want your computer not to sleep or hibernate. The reason behind it might be because you are using it for monitoring purpose. In my company there are a strict rule to forbid you to use third party application without their permission 😓. Another reason is because if you want to change some Group Policy Rule it takes a lot of time.
I recomended this application if you can use it in your environment. If you are stuck with the situation like me, fear not because i have a simple solution to this problem.
We could create a simple powershell script to emulate keypress and mouse movement.
Below is the code for it:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myshell = New-Object -com "Wscript.Shell"
while($true){
Start-Sleep -Seconds 60
$myshell.sendkeys(" ")
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) - 1) , $Pos.Y)
}
I will explain it one by one.
The first two line is to initiate dot NET library. The third one is to initiate com object.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$myshell = New-Object -com "Wscript.Shell"
Next we create iteration with 1 minute sleep interval. Sleep is necessary so your script won’t take too much CPU resource. Also sleep is good for your health 😅.
while($true){
Start-Sleep -Seconds 60
}
Next we will emulate key press using this command.
$myshell.sendkeys(" ")
Then we make our mouse to move 1 pixel and bring it back to the last pixel.
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y)
$Pos = [System.Windows.Forms.Cursor]::Position
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) - 1) , $Pos.Y)
Now you can save the script and execute it. If your company disallow powershell script execution then you can run the above script in regular powershell and you are good to go. Another reason to use it because you will always appear green in your company messanger application (in my case we are using Skype for Business). This is good if you’re working from home 😃.