How to Keep Your Screen Active with a PowerShell Script?

In this tutorial, I will explain how to create a PowerShell script that prevents your computer screen from going inactive or your PC from entering sleep mode. This is particularly useful during presentations, long-running tasks, or when you need to keep your computer awake for an extended period. Let me explain how to keep your screen active with a PowerShell script.

Keep Your Screen Active with a PowerShell Script

I recently encountered an issue where my computer would frequently lock the screen or enter sleep mode during lengthy conference calls and remote troubleshooting sessions. This interruption was frustrating. I needed a solution to keep my screen active without manually moving the mouse or pressing keys every few minutes.

To address this problem, I decided to create a PowerShell script that simulates a key press every 60 seconds. By automating this process, I could ensure my screen remained active and my PC stayed awake throughout my workday. Here’s how I did it:

Step 1: Create the PowerShell Script

First, I created a new PowerShell script file named KeepScreenActive.ps1. I used the following code:

$WShell = New-Object -com "Wscript.Shell"

while ($true)
{
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Seconds 60
}

Let’s break down the script:

  1. $WShell = New-Object -com "Wscript.Shell" creates a Windows scripting shell COM object, which allows us to simulate key presses.
  2. while ($true) starts an infinite loop that will continue running until manually stopped.
  3. $WShell.sendkeys("{SCROLLLOCK}") simulates pressing the Scroll Lock key. I chose this key because it has minimal impact on most applications.
  4. Start-Sleep -Seconds 60 pauses the script for 60 seconds before the next iteration of the loop.

Check out Get Computer Information Using PowerShell

Step 2: Run the PowerShell Script

To run the script, I opened a PowerShell window as an administrator and navigated to the directory where I saved the KeepScreenActive.ps1 file. Then, I executed the following command:

.\KeepScreenActive.ps1

The script started running, and my screen remained active as long as the PowerShell window was open.

Step 3: Enhancing the Script

To make the script more user-friendly and customizable, I added a few enhancements:

  1. Duration Parameter: I modified the script to accept a duration parameter, allowing users to specify how long the script should run. Here’s the updated script:
param(
  [Parameter(Mandatory=$true)]
  [int]$DurationMinutes
)

$WShell = New-Object -com "Wscript.Shell"
$EndTime = (Get-Date).AddMinutes($DurationMinutes)

while ((Get-Date) -lt $EndTime)
{
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Seconds 60
}

Now, users can run the script with a duration parameter, like this:

.\KeepScreenActive.ps1 -DurationMinutes 120

This command will keep the screen active for 2 hours (120 minutes).

  1. Background Execution: To avoid keeping a PowerShell window open, I used the Start-Process cmdlet to run the script in the background:
Start-Process powershell.exe -ArgumentList "-File .\KeepScreenActive.ps1 -DurationMinutes 120" -WindowStyle Hidden

This command starts a hidden PowerShell process that runs the script for the specified duration.

Conclusion

By creating a simple PowerShell script that simulates a key press every 60 seconds, I solved the problem of screen inactivity and PC sleep mode during my workday.

In this tutorial, I explained how to keep your screen active with a PowerShell script. Do let me know in the comments below if it helps.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.