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:
$WShell = New-Object -com "Wscript.Shell"creates a Windows scripting shell COM object, which allows us to simulate key presses.while ($true)starts an infinite loop that will continue running until manually stopped.$WShell.sendkeys("{SCROLLLOCK}")simulates pressing the Scroll Lock key. I chose this key because it has minimal impact on most applications.Start-Sleep -Seconds 60pauses 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.ps1The 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:
- 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 120This command will keep the screen active for 2 hours (120 minutes).
- Background Execution: To avoid keeping a PowerShell window open, I used the
Start-Processcmdlet to run the script in the background:
Start-Process powershell.exe -ArgumentList "-File .\KeepScreenActive.ps1 -DurationMinutes 120" -WindowStyle HiddenThis 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:
- Create a Shortcut to a Folder Using PowerShell
- Create Local Users in Windows Using PowerShell
- Create Desktop Shortcuts with Custom Icons Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.