Skip to content

Instantly share code, notes, and snippets.

@zudsniper
Created July 18, 2023 23:21
Show Gist options
  • Save zudsniper/1ff06f1f39f86c225cf90d1b124fd362 to your computer and use it in GitHub Desktop.
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.
# 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