7
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.

JAL
  • 41,701
  • 23
  • 172
  • 300
DDJ
  • 807
  • 5
  • 13
  • 31
  • 2
    Hi, why one line ? And why PowerShell ? These commands are very efficient by themselves :). – sodawillow Nov 18 '15 at 21:45
  • 2
    What do you mean "correctly"? Does it not already do exactly what you want? – Mathias R. Jessen Nov 18 '15 at 21:50
  • 1
    I'm agreeing with the first comment: why do this in PowerShell? It's been my experience that (unless you're familiar with the parsing engine) normal .exe commands (such as powercfg.exe) WORK in PowerShell, but there are some conventions that don't work so well either, and if you're not doing any form of checking / handling, it's not even worth using the shell for this. If, however, you'd like to continue using PowerShell (there's no need to in this case, but you certainly can), and want to have a single line, you could convert that single line to a script, and then just call the script. – PSGuy Nov 19 '15 at 05:31
  • I just used powershell because it always seems to go faster than command prompt but either one would really do – DDJ Nov 21 '15 at 17:49
  • I wanted to make sure I was doing my syntax right on here. – DDJ Nov 21 '15 at 17:50
  • Or if you could direct me to the best direction on how do multiple commands in one line. – DDJ Nov 21 '15 at 17:51
  • Which commands/syntax don't work as well in Powershell? Thanks for your input. – DDJ Nov 22 '15 at 14:44
  • That you all for your input! I appreciate your help – DDJ Nov 22 '15 at 14:52

2 Answers2

12
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.

techraf
  • 64,883
  • 27
  • 193
  • 198
DDJ
  • 807
  • 5
  • 13
  • 31
  • 1
    Why use C# to launch PowerShell to execute a batch file? – mdonoughe Sep 11 '18 at 17:26
  • 1
    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. – Sol Sep 13 '18 at 04:49
  • 1
    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? – user2173353 Feb 03 '20 at 09:58
  • I would guess in your actual drivers under Device Manager. I have seen sleep settings in there, but it depended on the drive/brand. – Sol Feb 04 '20 at 20:47
  • Sometimes people make interfaces in C#, that's what I was using this for. – DDJ Aug 09 '23 at 14:30
0

loved the answer this worked like a charm thanks for the reply I mean this answer

loved the answer this worked like a charm thanks for the reply I mean this answer

@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