Set a one line powershell for sleep and hibernate to turn off

32,383
var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
        newProcessInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
        newProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // hide processes as they happen
        newProcessInfo.Verb = "runas"; // run as administrator
        newProcessInfo.Arguments = @"-executionpolicy unrestricted -Command ""c:\power\powercfg.bat"""; //you can use the -noexit to troubleshoot and see the commands
        System.Diagnostics.Process.Start(newProcessInfo);

Batch file (c:\power\powercfg.bat):

@echo off
powercfg.exe -x -monitor-timeout-ac 0
powercfg.exe -x -monitor-timeout-dc 0
powercfg.exe -x -disk-timeout-ac 0
powercfg.exe -x -disk-timeout-dc 0
powercfg.exe -x -standby-timeout-ac 0
powercfg.exe -x -standby-timeout-dc 0
powercfg.exe -x -hibernate-timeout-ac 0
powercfg.exe -x -hibernate-timeout-dc 0

This worked best.

Share:
32,383
DDJ
Author by

DDJ

I am a Tech that mainly works on repairing PCs for a living, but want to learn more about programming to help make fun ideas become reality.

Updated on January 29, 2022

Comments

  • DDJ
    DDJ over 2 years
    powershell -Command "& {c:\windows\system32\powercfg.exe -change -monitor-timeout-ac 0; c:\windows\system32\powercfg.exe - change - monitor - timeout - dc 0; c:\windows\system32\powercfg.exe - change - disk - timeout - ac 0; c:\windows\system32\powercfg.exe - change - disk - timeout - dc 0; c:\windows\system32\powercfg.exe - change - standby - timeout - ac 0; c:\windows\system32\powercfg.exe - change - standby - timeout - dc 0; c:\windows\system32\powercfg.exe - change - hibernate - timeout - ac 0; c:\windows\system32\powercfg.exe - change - hibernate - timeout - dc 0 }"
    

    How would I write this code correctly? I want to set multiple power options at once to turn off hibernate and sleep modes.

  • mdonoughe
    mdonoughe almost 6 years
    Why use C# to launch PowerShell to execute a batch file?
  • Sol
    Sol almost 6 years
    Because you are making a user interface for other techs.... Powershell is the most powerful and it makes the programming consistent for other types of advanced commands.
  • user2173353
    user2173353 over 4 years
    Irrelevant, but I fail to understand why powercfg.exe -x -disk-timeout-ac should set the same setting for all drives. Any idea if it is possible to have different configurations per drive?
  • Sol
    Sol over 4 years
    I would guess in your actual drivers under Device Manager. I have seen sleep settings in there, but it depended on the drive/brand.