web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / [SOLUTION] Prevent Com...
Power Automate
Unanswered

[SOLUTION] Prevent Computer Sleep/Hibernation

(4) ShareShare
ReportReport
Posted on by 60

Awhile back I was looking to see if anyone had provided a solution for this. I suspected it was possible, but didn't want to have to recreate what someone else would've already solved, if it was available.

 

I never found anyone else's solution, as provided in a form that was tested and verified to work in Power Automate Desktop. I therefore attempted it, and wanted to provide my result in case anyone else finds it useful.

 

The trick is to take advantage of Scripting actions. In this case, PowerShell is typically the most directly associated option to a Windows host and access to lower level system options, so I started there. I found some PowerShell scripts via GitHub that offered the ability to prevent sleep, force "away mode" (seemingly some sort of sleep state), and prevent sleep while also preventing the monitor/display from turning off. With that, I was able to create an action that forced the running process to prevent the host it is running on from going to sleep.

 

The PowerShell script:

 

# https://gist.github.com/CMCDragonkai/bf8e8b7553c48e4f65124bc6f41769eb

param (
 [string]$Executable = $null,
 [ValidateSet('Away', 'Display', 'System')]$Option = 'System'
)

$Code=@'
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
'@

$ste = Add-Type -memberDefinition $Code -name System -namespace Win32 -passThru

# 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_CONTINUOUS = [uint32]"0x80000000"
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040"
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002"
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001"

Switch ($Option) {
 "Away" {$Setting = $ES_AWAYMODE_REQUIRED}
 "Display" {$Setting = $ES_DISPLAY_REQUIRED}
 "System" {$Setting = $ES_SYSTEM_REQUIRED}
}

$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $Setting)

 

 

The PAD action (as text; you can copy and paste the below directly into PAD):

 

@@copilotGeneratedAction: 'False'
Scripting.RunPowershellScript.RunPowershellScript Script: $'''# https://gist.github.com/CMCDragonkai/bf8e8b7553c48e4f65124bc6f41769eb

param (
 [string]$Executable = $null,
 [ValidateSet(\'Away\', \'Display\', \'System\')]$Option = \'System\'
)

$Code=@\'
[DllImport(\"kernel32.dll\", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);
\'@

$ste = Add-Type -memberDefinition $Code -name System -namespace Win32 -passThru

# 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_CONTINUOUS = [uint32]\"0x80000000\"
$ES_AWAYMODE_REQUIRED = [uint32]\"0x00000040\"
$ES_DISPLAY_REQUIRED = [uint32]\"0x00000002\"
$ES_SYSTEM_REQUIRED = [uint32]\"0x00000001\"

Switch ($Option) {
 \"Away\" {$Setting = $ES_AWAYMODE_REQUIRED}
 \"Display\" {$Setting = $ES_DISPLAY_REQUIRED}
 \"System\" {$Setting = $ES_SYSTEM_REQUIRED}
}

$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $Setting)''' ScriptOutput=> PowershellOutput

 

 

This will set the power state of the computer to disallow it from going to sleep while the EXE running the automation is active. Once the EXE is no longer active, the sleep/hibernate prevention will automatically end. You'll most likely want this placed at the beginning of your automation.

 

(I tried to get PAD to run a disable command using code from the linked resource, and it worked OUTSIDE of PAD, but for some reason would not halt the sleep prevention from inside of PAD. Since it auto-stops when PAD completes its flow, it was not an issue in my case.)

 

To test, and/or see what state the machine is currently in, and if any machines refuse to go to sleep (and you're convinced it was caused by this [it shouldn't be]), you can put your mind at ease by checking using the following command from an administrative command prompt:

 

powercfg -requests

 

 

Happy automating!

I have the same question (0)
  • trice602 Profile Picture
    16,164 Super User 2026 Season 1 on at

    Another easy button is to just open PowerPoint and present a blank presentation.  If you are presenting, your machine does not sleep.  I used this all the time on the weekends and overnight with flows, machine never sleeps.

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 875

#2
Valantis Profile Picture

Valantis 817

#3
Haque Profile Picture

Haque 481

Last 30 days Overall leaderboard