29 August 2016

PowerShell: Configuring Power Settings

This is another part of the Windows 10 project I am working on automating. This script will set the Windows 10 power settings. I created this, with the help of Sapien's PowerShell Studio that helped make this script much more robust, so that it can be executed during the image to configure the settings there. This allows me to take the settings out of GPO and put them on the local machine to help speed up the logon process. The users at the firm I work at do not have local administrator privileges, so this works great.

The advantage to using this script for configuring power settings, over using powercfg.exe directly, is that the script will verify the setting took place. It goes back and checks the new value to make sure it coincides with the desired value. If it was not successful, the script will return an error code 5, which I arbitrarily chose. This allows you to use the script in a build and it will report the error back to alert you at the end of the build if the power setting was unsuccessful.

The script can set individual power settings per the command line, you can hardcode the settings into the script (I commented out an example), or you can import a power scheme. I also included the feature to generate a detailed and formatted report of all power settings on a machine. The report not only displays to the screen, but it also generates a file named PowerSchemeReport.txt in the same directory as the script. I have included command line examples in the comments of the script.

Here is an example of the script generating a report:


Here is an example of setting the Monitor Timeout while plugged in to 120 minutes:


Here is an example of setting the power scheme from high performance to balanced:


Here is an example of the script importing a power scheme configuration file and setting it as the default:




You can download the file from here.


Set-PowerScheme.ps1

1:  <#  
2:       .SYNOPSIS  
3:            Set the Power Options  
4:         
5:       .DESCRIPTION  
6:            This script will set the preferred plan. It can also customize specific settings within a plan. The is an option to use the script for generating a report on the currently selected plan, along with all of the plan settings that is written both to the screen and to a log file. The script will exit with an error code 5 if any power setting failed. This allows for an error flag when used during a build.  
7:         
8:       .PARAMETER Balanced  
9:            Selects the balanced plan  
10:         
11:       .PARAMETER ConsoleTitle  
12:            Name for the PowerShell Console Title  
13:         
14:       .PARAMETER Custom  
15:            Enter a name to create a custom Power Plan  
16:         
17:       .PARAMETER HighPerformance  
18:            Selects the High Performance Plan  
19:         
20:       .PARAMETER ImportPowerSchemeFile  
21:            Import a power scheme file  
22:         
23:       .PARAMETER PowerSaver  
24:            Selects the Power Saver Plan  
25:         
26:       .PARAMETER PowerSchemeName  
27:            Name to use when renaming an imported scheme  
28:         
29:       .PARAMETER Report  
30:            Select this switch to generate a report of the currently selected plan  
31:         
32:       .PARAMETER SetPowerSchemeSetting  
33:            Set individual power scheme setting  
34:         
35:       .PARAMETER SetPowerSchemeSettingValue  
36:            Value associated with the Power Scheme Setting  
37:         
38:       .PARAMETER SetImportedPowerSchemeDefault  
39:            This is used in conjunction with the ImportPowerSchemeFile parameter. This tells the script to set the imported power scheme as the default.  
40:         
41:       .EXAMPLE  
42:            Set Power Settings to Balanced  
43:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -Balanced  
44:              
45:            Set Power Settings to High Performance  
46:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -HighPerformance  
47:              
48:            Set Power Settings to Power Saver  
49:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -PowerSaver  
50:              
51:            Generate a report named PowerSchemeReport.txt that resides in the same directory as this script. It contains a list of all power settings.  
52:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -Report  
53:              
54:            Set individual power scheme setting  
55:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -SetPowerSchemeSetting -MonitorTimeoutAC 120  
56:              
57:            Import power scheme file that resides in the same directory as this script and renames the scheme to the name defined under PowerSchemeName  
58:            powershell.exe -executionpolicy bypass -file Set-PowerScheme.ps1 -ImportPowerSchemeFile "CustomScheme.cfg" -PowerSchemeName "Custom"  
59:         
60:       .NOTES  
61:            ===========================================================================  
62:            Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.127  
63:            Created on:       8/16/2016 10:13 AM  
64:            Created by:       Mick Pletcher  
65:            Organization:  
66:            Filename:        Set-PowerScheme.ps1  
67:            ===========================================================================  
68:  #>  
69:  [CmdletBinding()]  
70:  param  
71:  (  
72:       [switch]  
73:       $Balanced,  
74:       [string]  
75:       $ConsoleTitle = 'PowerScheme',  
76:       [string]  
77:       $Custom,  
78:       [switch]  
79:       $HighPerformance,  
80:       [string]  
81:       $ImportPowerSchemeFile,  
82:       [switch]  
83:       $PowerSaver,  
84:       [string]  
85:       $PowerSchemeName,  
86:       [switch]  
87:       $Report,  
88:       [ValidateSet('MonitorTimeoutAC', 'MonitorTimeoutDC', 'DiskTimeoutAC', 'DiskTimeoutDC', 'StandbyTimeoutAC', 'StandbyTimeoutDC', 'HibernateTimeoutAC', 'HibernateTimeoutDC')][string]  
89:       $SetPowerSchemeSetting,  
90:       [string]  
91:       $SetPowerSchemeSettingValue,  
92:       [switch]  
93:       $SetImportedPowerSchemeDefault  
94:  )  
95:    
96:  function Get-PowerScheme {  
97:  <#  
98:       .SYNOPSIS  
99:            Get the currently active PowerScheme  
100:         
101:       .DESCRIPTION  
102:            This will query the current power scheme and return the GUID and user friendly name  
103:         
104:       .EXAMPLE  
105:            PS C:\> Get-PowerScheme  
106:         
107:       .NOTES  
108:            Additional information about the function.  
109:  #>  
110:         
111:       [CmdletBinding()][OutputType([object])]  
112:       param ()  
113:         
114:       #Get the currently active power scheme  
115:       $Query = powercfg.exe /getactivescheme  
116:       #Get the alias name of the active power scheme  
117:       $ActiveSchemeName = ($Query.Split("()").Trim())[1]  
118:       #Get the GUID of the active power scheme  
119:       $ActiveSchemeGUID = ($Query.Split(":(").Trim())[1]  
120:       $Query = powercfg.exe /query $ActiveSchemeGUID  
121:       $GUIDAlias = ($Query | where { $_.Contains("GUID Alias:") }).Split(":")[1].Trim()  
122:       $Scheme = New-Object -TypeName PSObject  
123:       $Scheme | Add-Member -Type NoteProperty -Name PowerScheme -Value $ActiveSchemeName  
124:       $Scheme | Add-Member -Type NoteProperty -Name GUIDAlias -Value $GUIDAlias  
125:       $Scheme | Add-Member -Type NoteProperty -Name GUID -Value $ActiveSchemeGUID  
126:       Return $Scheme  
127:  }  
128:    
129:  function Get-PowerSchemeSubGroupSettings {  
130:  <#  
131:       .SYNOPSIS  
132:            Get the Power Scheme SubGroup Settings  
133:         
134:       .DESCRIPTION  
135:            Retrieve all Settings and values within a subgroup  
136:         
137:       .PARAMETER Subgroup  
138:            Name and GUID of desired subgroup  
139:         
140:       .PARAMETER ActivePowerScheme  
141:            GUID and name of the active ActivePowerScheme  
142:         
143:       .EXAMPLE  
144:            PS C:\> Get-PowerSchemeSubGroupSettings -Subgroup $value1  
145:         
146:       .NOTES  
147:            Additional information about the function.  
148:  #>  
149:         
150:       [CmdletBinding()]  
151:       param  
152:       (  
153:            [ValidateNotNullOrEmpty()]$Subgroup,  
154:            [ValidateNotNullOrEmpty()][object]  
155:            $ActivePowerScheme  
156:       )  
157:         
158:       $Query = powercfg.exe /query $ActivePowerScheme.GUID $Subgroup.GUID  
159:       $Query = $Query | where { ((!($_.Contains($ActivePowerScheme.GUID))) -and (!($_.Contains($ActivePowerScheme.GUIDAlias)))) }  
160:       $Settings = @()  
161:       For ($i = 0; $i -lt $Query.Length; $i++) {  
162:            If ($Query[$i] -like "*Power Setting GUID:*") {  
163:                 $Setting = New-Object System.Object  
164:                 #Get the friendly name of the Power Setting  
165:                 $SettingName = $Query[$i].Split("()").Trim()  
166:                 $SettingName = $SettingName[1]  
167:                 #Get the alias of the power setting  
168:                 If ($Query[$i + 1] -like "*GUID Alias:*") {  
169:                      $SettingAlias = $Query[$i + 1].Split(":").Trim()  
170:                      $SettingAlias = $SettingAlias[1]  
171:                 } else {  
172:                      $SettingAlias = $null  
173:                 }  
174:                 #Get the GUID of the power setting  
175:                 $SettingGUID = $Query[$i].Split(":(").Trim()  
176:                 $SettingGUID = $SettingGUID[1]  
177:                 #Get the AC and DC power settings  
178:                 $j = $i  
179:                 Do {  
180:                      $j++  
181:                 }  
182:                 while ($Query[$j] -notlike "*Current AC Power Setting*")  
183:                 $SettingAC = $Query[$j].Split(":").Trim()  
184:                 $SettingAC = [Convert]::ToInt32($SettingAC[1], 16)  
185:                 $SettingDC = $Query[$j + 1].Split(":").Trim()  
186:                 $SettingDC = [Convert]::ToInt32($SettingDC[1], 16)  
187:                 $Setting | Add-Member -Type NoteProperty -Name Subgroup -Value $Subgroup.Subgroup  
188:                 $Setting | Add-Member -Type NoteProperty -Name Name -Value $SettingName  
189:                 $Setting | Add-Member -Type NoteProperty -Name Alias -Value $SettingAlias  
190:                 $Setting | Add-Member -Type NoteProperty -Name GUID -Value $SettingGUID  
191:                 $Setting | Add-Member -Type NoteProperty -Name AC -Value $SettingAC  
192:                 $Setting | Add-Member -Type NoteProperty -Name DC -Value $SettingDC  
193:                 $Settings += $Setting  
194:            }  
195:       }  
196:       Return $Settings  
197:  }  
198:    
199:  function Get-RelativePath {  
200:  <#  
201:       .SYNOPSIS  
202:            Get the relative path  
203:         
204:       .DESCRIPTION  
205:            Returns the location of the currently running PowerShell script  
206:         
207:       .NOTES  
208:            Additional information about the function.  
209:  #>  
210:         
211:       [CmdletBinding()][OutputType([string])]  
212:       param ()  
213:         
214:       $Path = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"  
215:       Return $Path  
216:  }  
217:    
218:  function Get-SubGroupsList {  
219:  <#  
220:       .SYNOPSIS  
221:            Generate a list of subgroups  
222:         
223:       .DESCRIPTION  
224:            This will generate a list of the subgroups within the designated power scheme  
225:         
226:       .PARAMETER ActivePowerScheme  
227:            GUID and name of the active ActivePowerScheme  
228:         
229:       .EXAMPLE  
230:            PS C:\> Get-SubGroupsList  
231:         
232:       .NOTES  
233:            Additional information about the function.  
234:  #>  
235:         
236:       [CmdletBinding()][OutputType([object])]  
237:       param  
238:       (  
239:            [ValidateNotNullOrEmpty()][object]  
240:            $ActivePowerScheme  
241:       )  
242:         
243:       #Get all settings for the active power scheme  
244:       $Query = powercfg.exe /query $ActivePowerScheme.GUID  
245:       #Get a list of the subgroups  
246:       $Subgroups = @()  
247:       for ($i = 0; $i -lt $Query.Length; $i++) {  
248:            If (($Query[$i] -like "*Subgroup GUID:*") -and ($Query[$i + 1] -notlike "*Subgroup GUID:*")) {  
249:                 $Subgroup = New-Object System.Object  
250:                 $SubgroupName = $Query[$i].Split("()").Trim()  
251:                 $SubgroupName = $SubgroupName[1]  
252:                 If ($Query[$i + 1] -like "*GUID Alias:*") {  
253:                      $SubgroupAlias = $Query[$i + 1].Split(":").Trim()  
254:                      $SubgroupAlias = $SubgroupAlias[1]  
255:                 } else {  
256:                      $SubgroupAlias = $null  
257:                 }  
258:                 $SubgroupGUID = $Query[$i].Split(":(").Trim()  
259:                 $SubgroupGUID = $SubgroupGUID[1]  
260:                 $Subgroup | Add-Member -Type NoteProperty -Name Subgroup -Value $SubgroupName  
261:                 $Subgroup | Add-Member -Type NoteProperty -Name Alias -Value $SubgroupAlias  
262:                 $Subgroup | Add-Member -Type NoteProperty -Name GUID -Value $SubgroupGUID  
263:                 $Subgroups += $Subgroup  
264:            }  
265:       }  
266:       Return $Subgroups  
267:  }  
268:    
269:  function Import-PowerScheme {  
270:  <#  
271:       .SYNOPSIS  
272:            Import a Power Scheme  
273:         
274:       .DESCRIPTION  
275:            Imports a power scheme configuration file  
276:         
277:       .PARAMETER File  
278:            Name of the configuration file. This must reside in the same directory as this script.  
279:         
280:       .PARAMETER PowerSchemeName  
281:            Desired name for the imported power scheme  
282:         
283:       .PARAMETER SetActive  
284:            Set the imported scheme to active  
285:         
286:       .EXAMPLE  
287:            PS C:\> Import-PowerScheme -File 'Value1'  
288:         
289:       .NOTES  
290:            Additional information about the function.  
291:  #>  
292:         
293:       [CmdletBinding()][OutputType([boolean])]  
294:       param  
295:       (  
296:            [ValidateNotNullOrEmpty()][string]  
297:            $File,  
298:            [ValidateNotNullOrEmpty()][string]  
299:            $PowerSchemeName,  
300:            [switch]  
301:            $SetActive  
302:       )  
303:         
304:       $RelativePath = Get-RelativePath  
305:       $File = $RelativePath + $File  
306:       #Get list of all power schemes  
307:       $OldPowerSchemes = powercfg.exe /l  
308:       #Filter out all data except for the GUID  
309:       $OldPowerSchemes = $OldPowerSchemes | where { $_ -like "*Power Scheme GUID*" } | ForEach-Object { $_ -replace "Power Scheme GUID: ", "" } | ForEach-Object { ($_.split("?("))[0] }  
310:       Write-Host "Importing Power Scheme....." -NoNewline  
311:       #Import Power Scheme  
312:       $Output = powercfg.exe -import $File  
313:       #Get list of all power schemes  
314:       $NewPowerSchemes = powercfg.exe /l  
315:       #Filter out all data except for the GUID  
316:       $NewScheme = $NewPowerSchemes | where { $_ -like "*Power Scheme GUID*" } | ForEach-Object { $_ -replace "Power Scheme GUID: ", "" } | ForEach-Object { ($_.split("?("))[0] } | where { $OldPowerSchemes -notcontains $_ }  
317:       If ($NewScheme -ne $null) {  
318:            Write-Host "Success" -ForegroundColor Yellow  
319:            $Error = $false  
320:       } else {  
321:            Write-Host "Failed" -ForegroundColor Red  
322:            $Error = $true  
323:       }  
324:       #Rename imported power scheme  
325:       Write-Host "Renaming imported power scheme to"$PowerSchemeName"....." -NoNewline  
326:       $Switches = "/changename" + [char]32 + $NewScheme.Trim() + [char]32 + [char]34 + $PowerSchemeName + [char]34  
327:       $ErrCode = (Start-Process -FilePath "powercfg.exe" -ArgumentList $Switches -WindowStyle Minimized -Wait -Passthru).ExitCode  
328:       $NewPowerSchemes = powercfg.exe /l  
329:       If ($ErrCode -eq 0) {  
330:            $Test = $NewPowerSchemes | where { $_ -like ("*" + $PowerSchemeName + "*") }  
331:            If ($Test -ne $null) {  
332:                 Write-Host "Success" -ForegroundColor Yellow  
333:                 $Error = $false  
334:            } else {  
335:                 Write-Host "Failed" -ForegroundColor Red  
336:                 $Error = $true  
337:                 Return $Error  
338:            }  
339:       }  
340:       Write-Host "Setting"$PowerSchemeName" to default....." -NoNewline  
341:       $Switches = "-setactive " + $NewScheme.Trim()  
342:       $ErrCode = (Start-Process -FilePath "powercfg.exe" -ArgumentList $Switches -WindowStyle Minimized -Wait -Passthru).ExitCode  
343:       $Query = powercfg.exe /getactivescheme  
344:       #Get the alias name of the active power scheme  
345:       $ActiveSchemeName = (powercfg.exe /getactivescheme).Split("()").Trim()[1]  
346:       If ($ActiveSchemeName -eq $PowerSchemeName) {  
347:            Write-Host "Success" -ForegroundColor Yellow  
348:            $Error = $false  
349:       } else {  
350:            Write-Host "Failed" -ForegroundColor Red  
351:            $Error = $true  
352:       }  
353:       Return $Error  
354:  }  
355:    
356:  function Publish-Report {  
357:  <#  
358:       .SYNOPSIS  
359:            Publish a Power Scheme Report  
360:         
361:       .DESCRIPTION  
362:            This will publish a report of the currently active power scheme, a list of the power scheme subgroups, and a list of all subgroup settings.  
363:         
364:       .EXAMPLE  
365:            PS C:\> Publish-Report  
366:         
367:       .NOTES  
368:            Additional information about the function.  
369:  #>  
370:         
371:       [CmdletBinding()]  
372:       param ()  
373:         
374:       #Get the relative path this script is being executed from  
375:       $RelativePath = Get-RelativePath  
376:       #Get the currently enabled power scheme data  
377:       $ActivePowerScheme = Get-PowerScheme  
378:       #Get a list of all available subgroups  
379:       $PowerSchemeSubGroups = Get-SubGroupsList -ActivePowerScheme $ActivePowerScheme  
380:       #Get a list of all settings under each subgroup  
381:       $PowerSchemeSettings = @()  
382:       for ($i = 0; $i -lt $PowerSchemeSubGroups.Length; $i++) {  
383:            $PowerSchemeSubGroupSettings = Get-PowerSchemeSubGroupSettings -ActivePowerScheme $ActivePowerScheme -Subgroup $PowerSchemeSubGroups[$i]  
384:            $PowerSchemeSettings += $PowerSchemeSubGroupSettings  
385:       }  
386:       #Define the Report text file to write to  
387:       $ReportFile = $RelativePath + "PowerSchemeReport.txt"  
388:       #Remove old report if it exists  
389:       If ((Test-Path $ReportFile) -eq $true) {  
390:            Remove-Item -Path $ReportFile -Force  
391:       }  
392:       #Generate Header for Power Scheme Report  
393:       $Header = "ACTIVE POWER SCHEME REPORT"  
394:       $Header | Tee-Object -FilePath $ReportFile -Append  
395:       $Header = "--------------------------------------------------------------------------------"  
396:       $Header | Tee-Object -FilePath $ReportFile -Append  
397:       #Get Active Power Scheme report  
398:       $Output = $ActivePowerScheme | Format-Table  
399:       #Write output to report screen and file  
400:       $Output | Tee-Object -FilePath $ReportFile -Append  
401:       #Generate Header for power scheme subgroups report  
402:       $Header = "POWER SCHEME SUBGROUPS REPORT"  
403:       $Header | Tee-Object -FilePath $ReportFile -Append  
404:       $Header = "--------------------------------------------------------------------------------"  
405:       $Header | Tee-Object -FilePath $ReportFile -Append  
406:       $Output = $PowerSchemeSubgroups | Format-Table  
407:       #Write output to report screen and file  
408:       $Output | Tee-Object -FilePath $ReportFile -Append  
409:       #Generate Header for power scheme subgroup settings report  
410:       $Header = "POWER SCHEME SUBGROUP SETTINGS REPORT"  
411:       $Header | Tee-Object -FilePath $ReportFile -Append  
412:       $Header = "--------------------------------------------------------------------------------"  
413:       $Header | Tee-Object -FilePath $ReportFile -Append  
414:       $Output = $PowerSchemeSettings | Format-Table  
415:       #Write output to report screen and file  
416:       $Output | Tee-Object -FilePath $ReportFile -Append  
417:  }  
418:    
419:  function Set-ConsoleTitle {  
420:  <#  
421:       .SYNOPSIS  
422:            Console Title  
423:         
424:       .DESCRIPTION  
425:            Sets the title of the PowerShell Console  
426:         
427:       .PARAMETER Title  
428:            Title of the PowerShell Console  
429:         
430:       .NOTES  
431:            Additional information about the function.  
432:  #>  
433:         
434:       [CmdletBinding()]  
435:       param  
436:       (  
437:            [Parameter(Mandatory = $true)][String]  
438:            $Title  
439:       )  
440:         
441:       $host.ui.RawUI.WindowTitle = $Title  
442:  }  
443:    
444:  function Set-PowerScheme {  
445:  <#  
446:       .SYNOPSIS  
447:            Set the power scheme to the specified scheme  
448:         
449:       .DESCRIPTION  
450:            Sets the power scheme to the specified scheme  
451:         
452:       .PARAMETER PowerScheme  
453:            Friendly power scheme name  
454:         
455:       .PARAMETER CustomPowerScheme  
456:            Create a custom power scheme  
457:         
458:       .EXAMPLE  
459:            PS C:\> Set-PowerScheme -PowerScheme 'Value1'  
460:         
461:       .NOTES  
462:            Additional information about the function.  
463:  #>  
464:         
465:       [CmdletBinding()][OutputType([boolean])]  
466:       param  
467:       (  
468:            [ValidateSet('Balanced', 'High Performance', 'Power Saver')][string]  
469:            $PowerScheme,  
470:            [string]  
471:            $CustomPowerScheme  
472:       )  
473:         
474:       #Get list of existing power schemes  
475:       $PowerSchemes = powercfg.exe /l  
476:       If ($PowerScheme -ne $null) {  
477:            #Filter out all schemes except for $PowerScheme and return the GUID  
478:            $PowerSchemes = ($PowerSchemes | where { $_ -like "*" + $PowerScheme + "*" }).Split(":(").Trim()[1]  
479:            #Set power scheme  
480:            $ActivePowerScheme = Get-PowerScheme  
481:            $ActivePowerScheme.PowerScheme  
482:            Write-Host "Setting Power Scheme from"$ActivePowerScheme.PowerScheme"to"$PowerScheme"....." -NoNewline  
483:            $Output = powercfg.exe -setactive $PowerSchemes  
484:            $ActivePowerScheme = Get-PowerScheme  
485:            If ($PowerScheme -eq $ActivePowerScheme.PowerScheme) {  
486:                 Write-Host "Success" -ForegroundColor Yellow  
487:                 Return $false  
488:            } else {  
489:                 Write-Host "Failed" -ForegroundColor Red  
490:                 Return $true  
491:            }  
492:       }  
493:  }  
494:    
495:  function Set-PowerSchemeSettings {  
496:  <#  
497:       .SYNOPSIS  
498:            Modify current power scheme  
499:         
500:       .DESCRIPTION  
501:            This will modify settings of the currently active power scheme.  
502:         
503:       .PARAMETER MonitorTimeoutAC  
504:            Modify the time until the screensaver turns on while plugged into AC outlet  
505:         
506:       .PARAMETER MonitorTimeoutDC  
507:            Modify the time until the screensaver turns on while on battery power  
508:         
509:       .PARAMETER DiskTimeoutAC  
510:            Time that windows will wait for a hard disk to respond to a command while plugged into AC outlet  
511:         
512:       .PARAMETER DiskTimeoutDC  
513:            Time that windows will wait for a hard disk to respond to a command while on battery power  
514:         
515:       .PARAMETER StandbyTimeoutAC  
516:            Amount of time before a computer is put on standby while plugged into AC outlet  
517:         
518:       .PARAMETER StandbyTimeoutDC  
519:            Amount of time before a computer is put on standby while on battery power  
520:         
521:       .PARAMETER HibernateTimeoutAC  
522:            Amount of time before a computer is put in hibernation while plugged into AC outlet  
523:         
524:       .PARAMETER HibernateTimeoutDC  
525:            Amount of time before a computer is put in hibernation while on battery power  
526:         
527:       .EXAMPLE  
528:            PS C:\> Set-PowerSchemeSettings -MonitorTimeoutAC $value1 -MonitorTimeoutDC $value2  
529:         
530:       .NOTES  
531:            Additional information about the function.  
532:  #>  
533:         
534:       [CmdletBinding()]  
535:       param  
536:       (  
537:            [string]  
538:            $MonitorTimeoutAC,  
539:            [string]  
540:            $MonitorTimeoutDC,  
541:            [string]  
542:            $DiskTimeoutAC,  
543:            [string]  
544:            $DiskTimeoutDC,  
545:            [string]  
546:            $StandbyTimeoutAC,  
547:            [string]  
548:            $StandbyTimeoutDC,  
549:            [string]  
550:            $HibernateTimeoutAC,  
551:            [string]  
552:            $HibernateTimeoutDC  
553:       )  
554:         
555:       $Scheme = Get-PowerScheme  
556:       If (($MonitorTimeoutAC -ne $null) -and ($MonitorTimeoutAC -ne "")) {  
557:            Write-Host "Setting monitor timeout on AC to"$MonitorTimeoutAC" minutes....." -NoNewline  
558:            $Switches = "/change" + [char]32 + "monitor-timeout-ac" + [char]32 + $MonitorTimeoutAC  
559:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\7516b95f-f776-4464-8c53-06167f40cc99\3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e"  
560:            $TestValue = $MonitorTimeoutAC  
561:            $PowerIndex = "ACSettingIndex"  
562:       }  
563:       If (($MonitorTimeoutDC -ne $null) -and ($MonitorTimeoutDC -ne "")) {  
564:            Write-Host "Setting monitor timeout on DC to"$MonitorTimeoutDC" minutes....." -NoNewline  
565:            $Switches = "/change" + [char]32 + "monitor-timeout-dc" + [char]32 + $MonitorTimeoutDC  
566:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\7516b95f-f776-4464-8c53-06167f40cc99\3c0bc021-c8a8-4e07-a973-6b14cbcb2b7e"  
567:            $TestValue = $MonitorTimeoutDC  
568:            $PowerIndex = "DCSettingIndex"  
569:       }  
570:       If (($DiskTimeoutAC -ne $null) -and ($DiskTimeoutAC -ne "")) {  
571:            Write-Host "Setting disk timeout on AC to"$DiskTimeoutAC" minutes....." -NoNewline  
572:            $Switches = "/change" + [char]32 + "disk-timeout-ac" + [char]32 + $DiskTimeoutAC  
573:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\0012ee47-9041-4b5d-9b77-535fba8b1442\6738e2c4-e8a5-4a42-b16a-e040e769756e"  
574:            $TestValue = $DiskTimeoutAC  
575:            $PowerIndex = "ACSettingIndex"  
576:       }  
577:       If (($DiskTimeoutDC -ne $null) -and ($DiskTimeoutDC -ne "")) {  
578:            Write-Host "Setting disk timeout on DC to"$DiskTimeoutDC" minutes....." -NoNewline  
579:            $Switches = "/change" + [char]32 + "disk-timeout-dc" + [char]32 + $DiskTimeoutDC  
580:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\0012ee47-9041-4b5d-9b77-535fba8b1442\6738e2c4-e8a5-4a42-b16a-e040e769756e"  
581:            $TestValue = $DiskTimeoutDC  
582:            $PowerIndex = "DCSettingIndex"  
583:       }  
584:       If (($StandbyTimeoutAC -ne $null) -and ($StandbyTimeoutAC -ne "")) {  
585:            Write-Host "Setting standby timeout on AC to"$StandbyTimeoutAC" minutes....." -NoNewline  
586:            $Switches = "/change" + [char]32 + "standby-timeout-ac" + [char]32 + $StandbyTimeoutAC  
587:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\238c9fa8-0aad-41ed-83f4-97be242c8f20\29f6c1db-86da-48c5-9fdb-f2b67b1f44da"  
588:            $TestValue = $StandbyTimeoutAC  
589:            $PowerIndex = "ACSettingIndex"  
590:       }  
591:       If (($StandbyTimeoutDC -ne $null) -and ($StandbyTimeoutDC -ne "")) {  
592:            Write-Host "Setting standby timeout on DC to"$StandbyTimeoutDC" minutes....." -NoNewline  
593:            $Switches = "/change" + [char]32 + "standby-timeout-dc" + [char]32 + $StandbyTimeoutDC  
594:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\238c9fa8-0aad-41ed-83f4-97be242c8f20\29f6c1db-86da-48c5-9fdb-f2b67b1f44da"  
595:            $TestValue = $StandbyTimeoutDC  
596:            $PowerIndex = "DCSettingIndex"  
597:       }  
598:       If (($HibernateTimeoutAC -ne $null) -and ($HibernateTimeoutAC -ne "")) {  
599:            Write-Host "Setting hibernate timeout on AC to"$HibernateTimeoutAC" minutes....." -NoNewline  
600:            $Switches = "/change" + [char]32 + "hibernate-timeout-ac" + [char]32 + $HibernateTimeoutAC  
601:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\238c9fa8-0aad-41ed-83f4-97be242c8f20\9d7815a6-7ee4-497e-8888-515a05f02364"  
602:            [int]$TestValue = $HibernateTimeoutAC  
603:            $PowerIndex = "ACSettingIndex"  
604:       }  
605:       If (($HibernateTimeoutDC -ne $null) -and ($HibernateTimeoutDC -ne "")) {  
606:            Write-Host "Setting hibernate timeout on DC to"$HibernateTimeoutDC" minutes....." -NoNewline  
607:            $Switches = "/change" + [char]32 + "hibernate-timeout-dc" + [char]32 + $HibernateTimeoutDC  
608:            $TestKey = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\" + $Scheme.GUID + "\238c9fa8-0aad-41ed-83f4-97be242c8f20\9d7815a6-7ee4-497e-8888-515a05f02364"  
609:            $TestValue = $HibernateTimeoutDC  
610:            $PowerIndex = "DCSettingIndex"  
611:       }  
612:       $ErrCode = (Start-Process -FilePath "powercfg.exe" -ArgumentList $Switches -WindowStyle Minimized -Wait -Passthru).ExitCode  
613:       $RegValue = (((Get-ItemProperty $TestKey).$PowerIndex) /60)  
614:       #Round down to the nearest tenth due to hibernate values being 1 decimal off  
615:       $RegValue = $RegValue - ($RegValue % 10)  
616:       If (($RegValue -eq $TestValue) -and ($ErrCode -eq 0)) {  
617:            Write-Host "Success" -ForegroundColor Yellow  
618:            $Errors = $false  
619:       } else {  
620:            Write-Host "Failed" -ForegroundColor Red  
621:            $Errors = $true  
622:       }  
623:       Return $Errors  
624:  }  
625:    
626:    
627:  cls  
628:  #Set Errors variable to false to begin the script with no errors  
629:  $Errors = $false  
630:  #Set the title of the PowerShell console  
631:  Set-ConsoleTitle -Title $ConsoleTitle  
632:    
633:  <#Hardcoded Power Scheme Settings  
634:  $Errors = Set-PowerSchemeSettings -MonitorTimeoutAC 120  
635:  $Errors = Set-PowerSchemeSettings -MonitorTimeoutDC 120  
636:  $Errors = Set-PowerSchemeSettings -DiskTimeOutAC 120  
637:  $Errors = Set-PowerSchemeSettings -DiskTimeOutDC 120  
638:  $Errors = Set-PowerSchemeSettings -StandbyTimeoutAC 120  
639:  $Errors = Set-PowerSchemeSettings -StandbyTimeoutDC 120  
640:  $Errors = Set-PowerSchemeSettings -HibernateTimeoutAC 60  
641:  $Errors = Set-PowerSchemeSettings -HibernateTimeoutDC 60  
642:  #>  
643:    
644:  #Generate a report if -Report is specified  
645:  If ($Report.IsPresent) {  
646:       Publish-Report  
647:  }  
648:  #Set the Power Scheme to Balanced  
649:  If ($Balanced.IsPresent) {  
650:       $Errors = Set-PowerScheme -PowerScheme 'Balanced'  
651:  }  
652:  #Set the Power Scheme to Power Saver  
653:  If ($PowerSaver.IsPresent) {  
654:       $Errors = Set-PowerScheme -PowerScheme 'Power Saver'  
655:  }  
656:  #Set the Power Scheme to High Performance  
657:  If ($HighPerformance.IsPresent) {  
658:       $Errors = Set-PowerScheme -PowerScheme 'High Performance'  
659:  }  
660:  #Set the Power Scheme to Custom  
661:  If (($Custom -ne $null) -and ($Custom -ne "")) {  
662:       $Errors = Set-PowerScheme -PowerScheme $Custom  
663:  }  
664:  #Import a power scheme  
665:  If (($ImportPowerSchemeFile -ne $null) -and ($ImportPowerSchemeFile -ne "")) {  
666:       If ($SetImportedPowerSchemeDefault.IsPresent) {  
667:            $Errors = Import-PowerScheme -File $ImportPowerSchemeFile -PowerSchemeName $PowerSchemeName -SetActive  
668:       } else {  
669:            $Errors = Import-PowerScheme -File $ImportPowerSchemeFile -PowerSchemeName $PowerSchemeName  
670:       }  
671:  }  
672:  #Set individual power scheme setting from command line  
673:  If (($SetPowerSchemeSetting -ne $null) -and ($SetPowerSchemeSetting -ne "")) {  
674:       switch ($SetPowerSchemeSetting) {  
675:            "MonitorTimeoutAC" { $Errors = Set-PowerSchemeSettings -MonitorTimeoutAC $SetPowerSchemeSettingValue }  
676:            "MonitorTimeoutDC" { $Errors = Set-PowerSchemeSettings -MonitorTimeoutDC $SetPowerSchemeSettingValue }  
677:            "DiskTimeOutAC" { $Errors = Set-PowerSchemeSettings -DiskTimeOutAC $SetPowerSchemeSettingValue }  
678:            "DiskTimeOutDC" { $Errors = Set-PowerSchemeSettings -DiskTimeOutDC $SetPowerSchemeSettingValue }  
679:            "StandbyTimeoutAC" { $Errors = Set-PowerSchemeSettings -StandbyTimeoutAC $SetPowerSchemeSettingValue }  
680:            "StandbyTimeoutDC" { $Errors = Set-PowerSchemeSettings -StandbyTimeoutDC $SetPowerSchemeSettingValue }  
681:            "HibernateTimeoutAC" { $Errors = Set-PowerSchemeSettings -HibernateTimeoutAC $SetPowerSchemeSettingValue }  
682:            "HibernateTimeoutDC" { $Errors = Set-PowerSchemeSettings -HibernateTimeoutDC $SetPowerSchemeSettingValue }  
683:       }  
684:  }  
685:  #Exit with an error code 5 if errors were encountered during any of the power settings  
686:  If ($Errors -eq $true) {  
687:       Exit 5  
688:  }  
689:    

0 comments:

Post a Comment