Skip to content

Instantly share code, notes, and snippets.

@vix597
Last active July 8, 2022 12:26
Show Gist options
  • Save vix597/9626aa2263142b59398dc90c7b40e7b8 to your computer and use it in GitHub Desktop.
Save vix597/9626aa2263142b59398dc90c7b40e7b8 to your computer and use it in GitHub Desktop.
Change laptop lid close action to "Do Nothing" when HDMI is connected
#
# Change a laptop's lid close action to "Do Nothing" for both AC and DC for the currently active power plan
# if an external display is connected via HDMI. If no HDMI is connected, set the close action to "Sleep" (default)
#
# Usage:
#
# ./powercfg-lidaction.ps1
#
# ./powercfg-lidaction.ps1 -Query
#
param (
[switch] $Query,
[string]$HDMIConnectedValue = 'Do Nothing',
[string]$HDMIDisconnectedValue = 'Hibernate'
)
function lidActionFromString($actionString) {
if ($actionString -eq "Sleep") {
return 1
} elseif ($actionString -eq "Hibernate") {
return 2
} elseif ($actionString -eq "Shutdown") {
return 3
} elseif ($actionString -eq "Do Nothing") {
return 0
}
Write-Error "Invalid action string: ""$actionString"""
Exit 1
}
function lidActionFromReg($actionRegValue) {
if ($actionRegValue -eq "0x1") {
return "Sleep"
} elseif ($actionRegValue -eq "0x2") {
return "Hibernate"
} elseif ($actionRegValue -eq "0x3") {
return "Shutdown"
} elseif ($actionRegValue -eq "0x0") {
return "Do Nothing"
}
Write-Error "Invalid action reg value: $actionRegValue"
Exit 1
}
function setLidAction($action) {
powercfg /setACvalueIndex scheme_current sub_buttons lidAction $action # On wall power (AC)
powercfg /setDCvalueIndex scheme_current sub_buttons lidAction $action # On Battery power (DC)
powercfg /setActive scheme_current
}
function getLidAction() {
# Get the active scheme GUID
$x = powercfg /GetActiveScheme
$activeSchemeGuid = $x.split()[3]
$subButtonsGuid = "4f971e89-eebd-4455-a8de-9e59040e7347"
$lidActionGuid = "5ca83367-6e45-459f-a27b-476b1d01c936"
# Query for the current powercfg setting for lid close action so we can toggle
$x = reg query HKLM\System\CurrentControlSet\Control\Power\User\PowerSchemes\$activeSchemeGuid\$subButtonsGuid\$lidActionGuid /v ACSettingIndex
return $x.split()[-2]
}
function isHDMIConnected() {
# Video Output Technology: HDMI
$D3DKMDT_VOT_HDMI = 5
# Check if HDMI is connected
$objWMi = get-wmiobject -namespace root\WMI -computername localhost -Query "Select * from WmiMonitorConnectionParams"
foreach ($obj in $objWmi) {
if ($obj.VideoOutputTechnology -eq $D3DKMDT_VOT_HDMI) {
return $true
}
}
return $false
}
$HDMIConnected = isHDMIConnected
$currentValue = getLidAction
if ($Query) {
# Log current state
$actionString = lidActionFromReg($currentValue)
Write-Output "HDMIConnected: $HDMIConnected"
Write-Output "Current setting: $actionString"
Exit
}
if ($HDMIConnected) {
Write-Output "Setting lid action to ""$HDMIConnectedValue""."
$lidAction = lidActionFromString($HDMIConnectedValue)
setLidAction($lidAction)
} else {
Write-Output "Setting lid action to ""$HDMIDisconnectedValue""."
$lidAction = lidActionFromString($HDMIDisconnectedValue)
setLidAction($lidAction)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment