1

I got fed up with my PC waking from sleep apparently at random intervals and did all the usual stuff, disabling wake timers, task schedules, disallowing LAN and other devices, running powercfg /sleepstudy etc. to no avail.

I found some scripts to disable all wake timers and ran that:

Get-ScheduledTask | ? { $_.Settings.WakeToRun -eq $true -and $_.State -ne "Disabled"} | % { $_.Settings.WakeToRun = $false; Set-ScheduledTask $_ }

Unfortunately, I also ran a script to disallow any device to wake the PC which appears to stall the sleep command until a key press and then shuts it down completely:

(/F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"

This produces several errors.

Although I can understand much of it, I don't have the programming skills to write a script to correct it :-(

Can anybody on here help? (can't restore a system image at the moment - I lent my backup drive+caddy to a buddy who's away)(OK - I'm a Muppett)

EDIT: OK - I ran
PS C:\WINDOWS\system32> @echo off

for /F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (powercfg -devicedisablewake "%%A")
which gave an error of a missing '(' after keyword 'for'. So, I put one in:
PS C:\WINDOWS\system32> @echo off for (/F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (powercfg -devicedisablewake "%%A").
This appears to have worked, although I still don't understand why, because there appears to be one '(' too many.

EDIT 2 Is there a way to reverse it? Can I just replace -devicedisableawake with -deviceenablewake ?

4
  • 2
    What are the "several errors" it produces?
    – Mokubai
    Commented Feb 18, 2018 at 19:29
  • @Mokubai do was unexpected at this time. :)
    – DavidPostill
    Commented Feb 18, 2018 at 19:43
  • One error was "F : The term '/F' is not recognized as the name of a cmdlet, function, script file, or operable program". Also, the original script had no ( at the start. Windows Powershell Admin said there was a missing ( at the start of the script, I didn't follow that because the ( made it unbalanced. I put one in and the script ran without an error - weird.
    – user57599
    Commented Feb 19, 2018 at 9:28
  • Another error was - The splatting operator '@' cannot be used to reference variables in an expression. '@echo' can be used only as an argument to a command. To reference variables in an expression use '$echo'.
    – user57599
    Commented Feb 19, 2018 at 9:48

1 Answer 1

0

This produces several errors.

Let's fix it step by step.

> (/F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"
do was unexpected at this time.

The first ( shouldn't be there.

> /F "tokens=*" %A in ('powercfg -devicequery wake_armed') do powercfg -devicedisablewake "%A"
'/F' is not recognized as an internal or external command,
operable program or batch file.

There is a missing for at the start.

From the command line it will only evaluate the first result, so you need to use a batch file.

The %s need to be replaced by %%s in a batch file.

Use the following batch file:

@echo off
for /F "tokens=*" %%A in ('powercfg -devicequery wake_armed') do (
  powercfg -devicedisablewake "%%A"
  )

Further Reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .