Skip to content

Instantly share code, notes, and snippets.

@matru
Forked from ThioJoe/DisableUSBPowerManagement.ps1
Last active September 26, 2025 23:43
Show Gist options
  • Select an option

  • Save matru/950798964684ec2fef3f5e40213d5f3a to your computer and use it in GitHub Desktop.

Select an option

Save matru/950798964684ec2fef3f5e40213d5f3a to your computer and use it in GitHub Desktop.
PowerShell script to disable Windows power management on all currently connected serial ports, including most (if not all) USB devices and adapters.
# PowerShell script to disable Windows power management on all currently connected serial ports, including USB adapters
# In simpler terms, it prevents Windows from turning off connected serial devices to save power.
# Equivalent to right-clicking on a serial port device in Device Manager > Properties > "Power Management" Tab > Unchecking "Allow the computer to turn off this device to save power."
# To run it you have a few options:
# .\SerialPowerMgmt.ps1 -Enable - this would enable power management (default)
# .\SerialPowerMgmt.ps1 -Disable - this would disable power management
# .\SerialPowerMgmt.ps1 -Disable -DryRun - just for testing, no change is done
param(
[switch]$Disable,
[switch]$Enable,
[switch]$DryRun
)
if (-not ($Disable -or $Enable)) {
Write-Error "You must specify either -Disable or -Enable."
exit 1
}
$serials = Get-CimInstance -ClassName Win32_SerialPort |
Select-Object Name, DeviceID, PNPDeviceID
$powerMgmt = Get-CimInstance -Namespace root\wmi -ClassName MSPower_DeviceEnable
foreach ($p in $powerMgmt) {
foreach ($s in $serials) {
if ([string]::IsNullOrWhiteSpace($s.PNPDeviceID)) { continue }
$pattern = [regex]::Escape($s.PNPDeviceID)
if ($p.InstanceName -match $pattern) {
$desired = if ($Disable) { $false } else { $true }
if ($p.Enable -ne $desired) {
if ($DryRun) {
Write-Host "[DRYRUN] Would set $($s.Name) ($($s.DeviceID)) to Enable=$desired"
} else {
try {
Set-CimInstance -InputObject $p -Property @{ Enable = $desired } -ErrorAction Stop
Write-Host "Set power mgmt for $($s.Name) ($($s.DeviceID)) to Enable=$desired"
} catch {
Write-Warning "Failed on $($s.Name) ($($s.DeviceID)): $($_.Exception.Message)"
}
}
} else {
Write-Host "Already in desired state: $($s.Name) ($($s.DeviceID)) (Enable=$desired)"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment