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


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 you may need to run a browser or other application.

As a side note, Presentation View has been around since Windows Vista days, but it is still present even in the latest Windows 10 build, 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 60 minutes, side effect is that a screenshot will overwrite the clipboard contents
# Change the color of error and warning text
# Valid colours: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White
# To see all colours:
# [enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_}

$opt = (Get-Host).PrivateData
$opt.WarningBackgroundColor = "DarkCyan"
$opt.WarningForegroundColor = "white"

write-warning "Your PC will not go to sleep whilst this window is open..."
Do {

Start-Sleep -Seconds 60

} 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.

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



Posted in PowerShell, Windows

Related Posts

Leave a Reply

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

%d bloggers like this: