Script to stop your screen going blank or PC going to sleep

PowerShell

How to stop your PC from going to sleep, even if you have group policies enforcing sleep settings. If you have a PC or laptop at work with settings or policies set to turn the screen off, lock the PC, or go to sleep, you may wish to prevent this from time to time. For example you may need to run a display in an office or shop window, or avoid embarrassment during presentations. On a standalone PC you can easily change the power settings to prevent your PC from going to sleep, but this is not ideal because you have to remember to switch it back again.

In a corporate environment you will likely have group policies enforcing these settings that you are unable to change. Having special GPOs applying to groups of machines is one solution, however this will often be time consuming and complicated to setup in a corporate network with security and change control.

PowerPoint does a good job of preventing the screen or PC from turning off during full screen presentation mode, however this doesn’t help if you need to run a browser or other application.

You can also use Presentation Mode to stop your PC going to sleep. Presentation Mode has been around since Windows Vista days and is still present Windows 10, but unfortunately does not work if you have group policies enforcing your settings. This is a shame as it is very easy to use (Right click on the Start menu, Mobility Centre, Turn on presentation view).

Fortunately, there is another easy way of doing this this that work in Windows 10 and earlier versions back to Windows 7 (you may need to alter the script slightly for earlier versions of PowerShell). So here is a way of doing it using a simple script:

Create a new text file in a folder called e.g. keep-alive, and call it keep-alive.ps1.

clear host
#
# Script to keep the PC alive, will prevent screen lock and sleep.
# Works by pressing Print Screen every 2 minutes, side effect is that a screenshot will overwrite the clipboard contents
#

write-warning "Your PC will not go to sleep whilst this window is open..."
Do {
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("{F1}")

Start-Sleep -Seconds 120

} While ($true)

Now create a new file in the same directory called keep-alive.bat and add the following:

powershell.exe -ExecutionPolicy Bypass -File .\keep-alive.ps1

Note that this will work on PCs that have Powershell scripts disabled, since it overrides this setting. It works by pressing the F1 key every 120 seconds. If you use the F1 key, you could find another key which does not do anything, see https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=windowsdesktop-6.0.

Run the .bat file and your PC will stay alive whilst the window is open.

Posted in PowerShell, Windows

Related Posts

3 Comments

  1. Keith Preseley

    Got the following on a windows 10 machine….

    C:\>powershell.exe -ExecutionPolicy Bypass -File .\keep-alive.ps1
    At C:\keep-alive.ps1:11 char:57
    + [void][System.Reflection.Assembly]::LoadWithPartialName(‘System.Win …
    + ~
    Missing ‘)’ in method call.
    At C:\keep-alive.ps1:11 char:57
    + … Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unexpected token ‘‘System.Windows.Forms’’ in expression or statement.
    At C:\keep-alive.ps1:10 char:4
    + Do {
    + ~
    Missing closing ‘}’ in statement block or type definition.
    At C:\keep-alive.ps1:11 char:83
    + … Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
    + ~
    Missing while or until keyword in do loop.
    At C:\keep-alive.ps1:11 char:83
    + … Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)
    + ~
    Unexpected token ‘)’ in expression or statement.
    At C:\keep-alive.ps1:12 char:43
    + [System.Windows.Forms.SendKeys]::SendWait(“{PRTSC}”)
    + ~
    Missing ‘)’ in method call.
    At C:\keep-alive.ps1:12 char:43
    + [System.Windows.Forms.SendKeys]::SendWait(“{PRTSC}”)
    + ~~~
    Unexpected token ‘“’ in expression or statement.
    At C:\keep-alive.ps1:12 char:56
    + [System.Windows.Forms.SendKeys]::SendWait(“{PRTSC}”)
    + ~
    Unexpected token ‘)’ in expression or statement.
    At C:\keep-alive.ps1:16 char:1
    + } While ($true)
    + ~
    Unexpected token ‘}’ in expression or statement.
    At C:\keep-alive.ps1:16 char:16
    + } While ($true)
    + ~
    Missing statement body in while loop.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

  2. msdadmin

    Characters getting messed up after copy/paste, delete and recreate the single and double quotes should fix that.

  3. Icke

    That is because of a (copy paste error)? by the author. The script on this page uses some strange ‘ and ” characters. Just replace them with the correct one

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.