Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script that creates a scheduled task to keep your RDP session alive to work around nonsensical GPOs that inhibit productivity
Keeps you productive by spoofing activity to prevent GPO idle timeouts, RDP disconnects, sleep, etc.
This script creates a Scheduled Task that runs at login which uses Kernel SetThreadExecutionState to prevent GPOs
from disconnecting your RDP session. Will also prevent sleeping/screensavers/display timeouts
See the example below for a one liner that will download and execute this script directly from GitHub!
- This will most likely be frowned upon by your security team. Please do not break any rules/compliance
- I am not responsible for any security issues that arise from misuse of this script (session hijacking, etc). Always log off when idle
- If run on a laptop/desktop, it will use more energy and prevent screen timeouts / sleep! Be environmentally conscious!
Version: 1.0.0
Author: jcefoli
Creation Date: 1/5/2021
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(''))
$dateStamp = Get-Date -Format "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffff"
$currentUserSID = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
$schTaskXML = @"
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="">
<Principal id="Author">
<Actions Context="Author">
<Arguments>-WindowStyle Hidden -File "C:\Users\$($env:USERNAME)\keepalive.ps1"</Arguments>
$rdpKeepalivePS1 = @"
`$host.ui.RawUI.WindowTitle = "Idle Keepalive"
`$dotNetCode = @'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
`$ste = Add-Type -memberDefinition `$dotNetCode -name System -namespace Win32 -passThru
`$ES_CONTINUOUS = [uint32]"0x80000000" #Requests that the other EXECUTION_STATE flags set remain in effect until SetThreadExecutionState is called again with the ES_CONTINUOUS flag set and one of the other EXECUTION_STATE flags cleared.
`$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040" #Requests Away Mode to be enabled.
`$ES_DISPLAY_REQUIRED = [uint32]"0x00000002" #Requests display availability (display idle timeout is prevented).
`$ES_SYSTEM_REQUIRED = [uint32]"0x00000001" #Requests system availability (sleep idle timeout is prevented).
Write-Verbose "Power Plan suspended with option: `$option"
`$ste::SetThreadExecutionState(`$ES_CONTINUOUS -bor `$ES_SYSTEM_REQUIRED -bor `$ES_DISPLAY_REQUIRED)
read-host "Keepalive active. Press any key to quit"
Write-Verbose "Power Plan suspension ended"
#powercfg -requests
# Replacements
$schTaskXML = $schTaskXML -replace "<Date>CHANGEME</Date>", "<Date>$dateStamp</Date>"
$schTaskXML = $schTaskXML -replace "<Author>CHANGEME</Author>", "<Author>$($env:USERDOMAIN)\$($env:USERNAME)</Author>"
$schTaskXML = $schTaskXML -replace "<UserId>CHANGEME</UserId>", "<UserId>$($env:USERDOMAIN)\$($env:USERNAME)</UserId>"
$schTaskXML = $schTaskXML -replace "<UserId>CHANGEME_SID</UserId>", "<UserId>$($currentUserSID)</UserId>"
# Write Necessary Files
$rdpKeepalivePS1 | Out-File -FilePath "C:\Users\$($env:USERNAME)\keepalive.ps1" -Encoding ascii
$schTaskXML | Out-File -FilePath "C:\Users\$($env:USERNAME)\keepalive.xml" -Encoding ascii
# Schedule the task
. schtasks /delete /tn "RDP Keepalive" /f
. schtasks /create /xml "$home\keepalive.xml" /tn "RDP Keepalive" /IT
# Cleanup
Remove-Item -Path "$home\keepalive.xml" -Force

This comment has been minimized.

Copy link

@Lonely16gbRam Lonely16gbRam commented May 21, 2021

How to use pls


This comment has been minimized.

Copy link
Owner Author

@jcefoli jcefoli commented May 24, 2021

How to use pls

If you copy and paste Line 23 into an Administrator elevated powershell, it will download the components needed to create a Scheduled Task that runs at logon of the current user, which will invoke a powershell script to prevent idle every time you log on

Although this is safe, I really don't recommend running things you find on the internet without fully understanding all of the code and what it's doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment