从命令行更改关闭盖子的操作?

从命令行更改关闭盖子的操作?

我想知道是否有人知道一个实用程序或命令,可以在 Windows 中单击一次更改此设置。我经常需要在笔记本电脑上更改它,无论我希望它在盖子关闭时不执行任何操作还是进入睡眠状态。

我确信可以通过命令行进行某种改变。

答案1

取自设置盖上电源选项。本页上还有一两个脚本,但在我看来,下面重现的方法是最好的。


您可以通过命令来设置powercfg

预配置方案具有以下 GUID:

Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver)

我将在示例中使用平衡方案,但您可以使用以下提供的 GUID:

powercfg -GETACTIVESCHEME

您可以通过使用方案 GUID 运行查询命令来找到子组和电源设置的 GUID 以及每个电源设置的索引值:

powercfg -Q 381b4222-f694-41f0-9685-ff5bb260df2e

查看输出,您将发现您想要的子组 GUID 是:

Subgroup GUID: 4f971e89-eebd-4455-a8de-9e59040e7347  (Power buttons and lid)

和电源设置:

Power Setting GUID: 5ca83367-6e45-459f-a27b-476b1d01c936  (Lid close action)

使用索引选项:

Possible Setting Index: 000
Possible Setting Friendly Name: Do nothing
Possible Setting Index: 001
Possible Setting Friendly Name: Sleep
Possible Setting Index: 002
Possible Setting Friendly Name: Hibernate
Possible Setting Index: 003
Possible Setting Friendly Name: Shut down

因此,为了将系统配置为在盖子关闭时关闭,您可以运行:

powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3
powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 3

笔记:这两行是完全相同的, 除了:

  • ACSETACVALUEINDEX插入“行动,并且;
  • DCSETDCVALUEINDEX电池“ 行动。

附加说明

这个帖子

通过命令行进行的设置必须随后

powercfg -setactive <GUID>`

也就是说,对于“平衡”

powercfg -setactive 381b4222-f694-41f0-9685-ff5bb260df2e

请注意,我没有使用块引用,因为它会弄乱相当长的代码行。因此,为了格式化,我没有使用块引用。

答案2

下面似乎是从命令行更改行为的最简单且易读的方法当前有效的电源方案(谢谢w17t 的回答这篇 TenForums 帖子):

关闭盖子时不做任何事.bat

::Do nothing when you close the lid
powercfg /setACvalueIndex scheme_current sub_buttons lidAction 0
powercfg /setDCvalueIndex scheme_current sub_buttons lidAction 0

::Re-activate current scheme to make settings take effect immediately
powercfg /setActive scheme_current

GoToSleepWhenClosingTheLid.bat

::Go to sleep/standby mode when you close the lid
powercfg /setACvalueIndex scheme_current sub_buttons lidAction 1
powercfg /setDCvalueIndex scheme_current sub_buttons lidAction 1

::Re-activate current scheme to make settings take effect immediately
powercfg /setActive scheme_current

答案3

这是使用 WMI 的另一种策略Power Policy Provider课程和Cim Cmdlets模块

$powerNamespace = @{ Namespace = 'root\cimv2\power' }

# https://docs.microsoft.com/en-us/windows-hardware/customize/power-settings/power-button-and-lid-settings-lid-switch-close-action
enum LidAction {
    DoNothing = 0
    Sleep = 1
    Hibernate = 2
    ShutDown = 3
}

enum PowerSupply {
    AC # Plugged In
    DC # On Battery
}

# get active plan
$curPlan = Get-CimInstance @powerNamespace -Class Win32_PowerPlan -Filter "IsActive = TRUE"

# get lid closed setting
$lidSetting = Get-CimInstance @powerNamespace -ClassName Win32_Powersetting -Filter "ElementName = 'Lid close action'"

# get guids
$planGuid = [Regex]::Matches($curPlan.InstanceId, "{.*}" ).Value
$lidGuid = [Regex]::Matches($lidSetting.InstanceID, "{.*}" ).Value

# get plugged in lid setting    
$pluggedInLidSetting = Get-CimInstance @powerNamespace -ClassName Win32_PowerSettingDataIndex `
    -Filter "InstanceID = 'Microsoft:PowerSettingDataIndex\\$planGuid\\$([PowerSupply]::AC)\\$lidGuid'"

# set lid action to do nothing
$pluggedInLidSetting | Set-CimInstance -Property @{ SettingIndexValue = [int][LidAction]::DoNothing }

# refresh to activate
$curPlan | Invoke-CimMethod -MethodName Activate

进一步阅读

答案4

powercfg支持 guid 的别名(方案名称、组等)

@echo off

powercfg /s scheme_min
rem scheme_min is high performance

powercfg /setacvalueindex scheme_min sub_buttons lidaction 0
rem under buttons group; plugged in

powercfg /setdcvalueindex scheme_min sub_buttons lidaction 0

rem dc means on battery

rem check with powercfg.cpl gui

rem use powercfg /aliases for aliases instead of guid numbers

rem query with powercfg /q

上述脚本将切换到高性能方案,并将When I close the lid:其下的盖子动作()设置None为两种情况(插入电源或使用电池时)。

它基本上只有三行代码,其余的几乎只是注释(雷姆方舟)。

相关内容