Created
July 18, 2023 23:21
-
-
Save zudsniper/1ff06f1f39f86c225cf90d1b124fd362 to your computer and use it in GitHub Desktop.
π Sleep time? or not? configure whether or not your Windows 10 PC can sleep.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sleep_schedule.ps1 | |
param ( | |
[Parameter(Mandatory=$false)] | |
[ValidateSet("0","1","true","false","yes","no")] | |
[string]$input | |
) | |
function ConvertTo-Boolean { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$input | |
) | |
switch -regex ($input) { | |
"^(1|true|yes)$" { return $true } | |
"^(0|false|no)$" { return $false } | |
} | |
} | |
function Toggle-Sleep { | |
param ( | |
[Parameter(Mandatory=$true)] | |
[bool]$enable | |
) | |
if ($enable) { | |
powercfg -h on | |
Write-Host "Sleep: ON" -ForegroundColor Green | |
} else { | |
powercfg -h off | |
Write-Host "Sleep: OFF" -ForegroundColor Red | |
} | |
} | |
$oldValue = (powercfg -a | Select-String "Hibernate") -ne $null | |
Write-Host "Old Value: " -NoNewline | |
if ($oldValue) { Write-Host "ON" -ForegroundColor Green } else { Write-Host "OFF" -ForegroundColor Red } | |
if ($input -ne $null) { | |
$newValue = ConvertTo-Boolean -input $input | |
} else { | |
$newValue = -not $oldValue | |
} | |
Toggle-Sleep -enable $newValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment