-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSet-SleepConfiguration.ps1
87 lines (78 loc) · 4.24 KB
/
Set-SleepConfiguration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function Set-SleepConfiguration {
<#
.SYNOPSIS
Configures sleep, hibernate, and fast startup settings on the system.
.DESCRIPTION
This function allows you to manage power settings such as sleep, hibernate, and fast startup modes. It provides options to enable or disable sleep, configure custom sleep timeouts, check current sleep settings, enable or disable hibernate, and enable fast startup for faster boot times.
.EXAMPLE
Set-SleepConfiguration -EnableSleep
Set-SleepConfiguration -SleepTimeout 300 -EnableSleep
Set-SleepConfiguration -DisableSleep
Set-SleepConfiguration -CheckSleep
Set-SleepConfiguration -EnableHibernate
Set-SleepConfiguration -DisableHibernate
Set-SleepConfiguration -EnableFastStartup
.NOTES
Version: 0.1.1
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, HelpMessage = "Enables sleep mode with a default or specified timeout. Use this to configure the system's sleep functionality")]
[switch]$EnableSleep,
[Parameter(Mandatory = $false, HelpMessage = "Specifies the sleep timeout in seconds. Must be a positive integer and used with -EnableSleep")]
[int]$SleepTimeout,
[Parameter(Mandatory = $false, HelpMessage = "Disables sleep mode entirely, preventing the system from entering sleep mode")]
[switch]$DisableSleep,
[Parameter(Mandatory = $false, HelpMessage = "Displays the current sleep settings, including timeouts for AC (plugged-in) and DC (battery) power modes")]
[switch]$CheckSleep,
[Parameter(Mandatory = $false, HelpMessage = "Enables hibernate mode, which saves the system state to disk and powers off to conserve energy")]
[switch]$EnableHibernate,
[Parameter(Mandatory = $false, HelpMessage = "Disables hibernate mode and deletes the hibernate file to free up disk space")]
[switch]$DisableHibernate,
[Parameter(Mandatory = $false, HelpMessage = "Enables fast startup, a hybrid feature that speeds up boot times by combining hibernate and shutdown behaviors")]
[switch]$EnableFastStartup
)
try {
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
throw "This function requires administrative privileges. Please run PowerShell as an administrator."
}
if ($EnableSleep -and $DisableSleep) {
throw "You cannot use -EnableSleep and -DisableSleep together!"
}
if ($EnableSleep -and $SleepTimeout -le 0) {
throw "When using -SleepTimeout with -EnableSleep, specify a positive timeout value in seconds!"
}
if ($EnableSleep) {
$Timeout = if ($SleepTimeout -gt 0) { $SleepTimeout } else { 900 }
powercfg /change standby-timeout-ac $Timeout
powercfg /change standby-timeout-dc $Timeout
Write-Host "Sleep enabled with a timeout of $Timeout seconds." -ForegroundColor Cyan
}
if ($DisableSleep) {
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
Write-Host "Sleep mode disabled." -ForegroundColor Cyan
}
if ($CheckSleep) {
$CurrentSettingAC = powercfg /query SCHEME_CURRENT SUB_SLEEP STANDBYIDLE | Select-String "(AC)" | ForEach-Object { $_ -replace '.*:\s+', '' }
$CurrentSettingDC = powercfg /query SCHEME_CURRENT SUB_SLEEP STANDBYIDLE | Select-String "(DC)" | ForEach-Object { $_ -replace '.*:\s+', '' }
Write-Host "Current sleep timeout (AC): $CurrentSettingAC seconds" -ForegroundColor Cyan
Write-Host "Current sleep timeout (DC): $CurrentSettingDC seconds" -ForegroundColor Cyan
}
if ($EnableHibernate) {
powercfg /hibernate on
Write-Host "Hibernate mode enabled." -ForegroundColor Cyan
}
if ($DisableHibernate) {
powercfg /hibernate off
Write-Host "Hibernate mode disabled." -ForegroundColor Cyan
}
if ($EnableFastStartup) {
powercfg /h on
Write-Host "Fast startup enabled." -ForegroundColor Cyan
}
}
catch {
Write-Error "An error occurred: $_"
}
}