Skip to content

Instantly share code, notes, and snippets.

@albertopasqualetto
Last active July 17, 2024 12:46
Show Gist options
  • Save albertopasqualetto/79a679e1627cfd5c3a3d62e572a71752 to your computer and use it in GitHub Desktop.
Save albertopasqualetto/79a679e1627cfd5c3a3d62e572a71752 to your computer and use it in GitHub Desktop.
Set Power Scheme based on HDMI monitor connected or not, to be used with scheduler
# Define the GUIDs for the power schemes
$HDMIScheme = "10c2d095-ea2f-46c9-8f8f-ce1e5a2d2e4d"
$normalScheme = "381b4222-f694-41f0-9685-ff5bb260df2e"
# Function to set the power scheme
function Set-PowerScheme {
param (
[string]$schemeGuid
)
powercfg -setactive $schemeGuid
}
# Get the list of connected monitors using
$monitors = Get-CimInstance -Namespace root\wmi -ClassName WmiMonitorBasicDisplayParams
# $monitors = Get-PnpDevice -class "monitor"
# Check if any monitor is connected via HDMI
$hdmiConnected = $false
foreach ($monitor in $monitors) {
# $monitorID = $monitor.InstanceId
$monitorID = $monitor.InstanceName.Split("_")[0]
# Using PnPDevice to check if it is an HDMI connection
$pnpDevice = Get-PnpDevice -InstanceId $monitorID
if ($pnpDevice.FriendlyName -match "HDMI") {
$hdmiConnected = $true
break
}
}
if ($hdmiConnected) {
Set-PowerScheme -schemeGuid $HDMIScheme
} else {
Set-PowerScheme -schemeGuid $normalScheme
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment