I am troubleshooting a problem with a device keeping a Windows 10 PC awake using the "powercfg -requests" command. I have a BAT file containing the following so it can be run quickly via shortcut with run as admin properties:
cmd /k powercfg -requests
This works, but in addition to keeping the window open, I'd like to be able to keep sending "powercfg -requests" by hitting arrow up/enter when needed, as would normally happen if I opened a command prompt and typed it manually.
I've searched quite a bit here, and aside from making it repeat automatically, I haven't found anything other than sending arrow keystokes in a BAT file.
I did see something which implied it might be possible to print the command so it's entered at the prompt after the command has executed, which would be fine too, but haven't had much luck.
Is this possible?
CodePudding user response:
Sorry, there is not a simple way...
This works:
@if (@CodeSection == @Batch) @then
@echo off
start "" /B cmd /K
timeout /T 1
CScript //nologo //E:JScript "%~F0" "powercfg -requests{ENTER}"
goto :EOF
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
For a further description on the method used, see this answer
CodePudding user response:
Use PowerShell instead. You can right click on Desktop > New Shortcut and paste this then save
powershell -NoExit -Command "Add-Content -Path (Get-PSReadlineOption).HistorySavePath 'powercfg -requests'; Invoke-Expression 'powercfg -requests'"
It's better to create a new shortcut because you can set it to always run as admin although it's still possible to put it in a *.bat/*.ps1 file
You can also shorten it like this
powershell -NoE -C "$c = 'powercfg -requests'; ac (Get-PSReadlineOption).HistorySavePath $c; iex $c"
PowerShell is far more flexible and powerful than batch. It also has a persistent history so it's far more convenient to user powershell. You may not even need this and just press Ctrl R to search through history for the powercfg
command just like in bash