Keep your Windows sessions alive and ready with this PowerShell script

Make sure your Windows systems are fully available and ready to roll with help from a simple PowerShell script.

Powershell

Image: iStock/BestForBest

Those of us who rely on and administer Windows systems for work are well aware of the various benefits and challenges bestowed upon us by Microsoft. For the most part, I'm satisfied with the default Windows settings, but every once in a while I need to make changes to my screen saver, sleep mode and screen lock settings for maximum efficiency. 

It's easy enough to do this if you're an administrator on the system (such as your personal computer).

SEE: 20 good habits network administrators need--and 10 habits to break (free PDF) (TechRepublic)

How to adjust Screen Saver settings

Right-click Start, go to Settings, search for Turn Screen Saver On Or Off and make the desired changes (Figure A).

Figure A

screensaver.jpg

How to adjust Sleep Mode settings

Right-click Start, go to Settings, choose System and then Power & Sleep and make the desired changes (Figure B).

Figure B

power-and-sleep.jpg

How to adjust Screen Lock settings using this PowerShell script

This handy TechRepublic article explains how you can make a necessary registry change then tweak this setting. It also provides a reliable registry file you can import to your computer to make the change for you.

If you're using a company-owned system or one you own that operates under a Bring Your Own Device policy, likely there will be centralized controls applied to this instance of Windows that you can't control–namely, automated screen locking, remote desktop session logoffs or sleep mode power options. All of these kick in after a period of inactivity, and it can be frustrating and time consuming to have to enter passwords repeatedly, reconnect to lost sessions or wait for a system to come out of sleep mode so you can resume your work.

With that in mind, this PowerShell script can help keep your Windows logons active and ready for work. It simply activates then deactivates the Scroll Lock button every four minutes. This sends a signal to the operating system that someone (technically something) is using the computer and thus your session remains alive.

I want to stress that I don't recommend circumventing security settings mandated for business operations–not only might that violate policy, but it can get workers in trouble. However, for a work-from-home environment, or when connecting to a remote Windows system via Remote Desktop, this script can be a safe blessing to help maintain productivity. Just be mindful of the fact if you run it on your local computer your Windows sessions will be fully available to anyone who accesses the keyboard and mouse, whether it's you or not. But you can still lock your screen using the Windows-L key combination and then unlock it with your password.

SEE: Microsoft PowerShell: Learn how to automate your workday (TechRepublic Academy)

To use the script, copy the code below, open up Notepad (right-click Start, choose Run, enter notepad and hit Enter) and paste it into the text editor. Save the script as "keepalive" (or whatever name you choose) and add the .ps1 extension. Note: You should set the Save As Type field to All Files, lest Notepad save the script as keepalive.ps1.txt (Figure C).

Figure C

saveas.jpg

Right-click the script file and choose Run With PowerShell to run it (or copy it to the target Windows machine you wish to run it on). You will see the Windows PowerShell screen (Figure D).

Figure D

script.jpg

The script must remain running in order to work for you, but you can minimize this window.

SEE: Checklist: Server inventory (TechRepublic Premium)

If you have issues running it with the PowerShell executable, try PowerShell Integrated Scripting Environment (ISE). Open the Start Menu, scroll down to Windows PowerShell, expand that folder and open Windows PowerShell ISE. Browse to the script file using File then Open and double-click it and then click the green Play icon in the toolbar so it runs (Figure E).

Figure E

scriptise.jpg

Clear-Host

Echo "Keep alive w/ scroll lock..."

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

while ($true)

{

$WShell.sendkeys("{SCROLLLOCK}")

Start-Sleep -Milliseconds 100

$WShell.sendkeys("{SCROLLLOCK}")

Start-Sleep -Seconds 240

}

Also see