Systems Administrators love to use feature-rich tools to set up and manage their devices on the network with the desired configuration starting from Group Policy to MS ConfigMgr / System Center Configuration Manager(SCCM). These solutions are robust, complex and but they can be also very expensive to maintain and implement correctly in every scenario.
In this article, we will play with Power Options and with Powershell.
What PowerShell is bringing to the table is, first of all, being included in the OS (for free) and can be used either for simple or complex scenarios depending on your capabilities. This degree of the flexibility to check if everything is working as expected in few lines of code and even to correct issues quickly it depends on the software developer or system engineer that writes the code.
Imagine checking if power option settings are applied correctly to every windows os according to the configuration for the Standard Operating Environment (SOE) image/set-up.
This script will help us to get the power options of a remote host.
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 |
#Requires -RunAsAdministrator function Get-PowerOption{ <# .Synopsis Get the power option of a host. .Description Get the power option of a host. .Example PS C:\Windows\system32> Get-PowerOption box1 WARNING: *** Starting WinRM service on box1 Existing Power Schemes (* Active) ----------------------------------- Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) * Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) Power Scheme GUID: db310065-829b-4671-9647-2261c00e86ef (High Performance (ConfigMgr)) WARNING: *** Stopping WinRM service on box1 .Example get-adcomputer -searchbase ‘OU=workstations,dc=contoso,dc=com’ -filter * -property * | select name | Get-PowerOption Get-PowerOption for all the workstation in AD. .Notes Author: Paolo Frigo - https://www.scriptinglibrary.com #> Param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [Alias('name')] [string[]]$ComputerName ) process{ $winRmNotRunning =(Get-Service -ComputerName $ComputerName winrm).Status -eq "Stopped" if ($winRmNotRunning -eq "True"){ Get-Service -ComputerName $ComputerName winrm | Start-Service Write-Warning "*** Starting WinRM service on $ComputerName" } $RemoteSession = New-PSSession -ComputerName $ComputerName invoke-command -Session $RemoteSession -ScriptBlock { powercfg /l } Get-Service -ComputerName $ComputerName winrm | Stop-Service Write-Warning "*** Stopping WinRM service on $ComputerName" Remove-PSSession -Session $RemoteSession } } |
This one will set remotely the power options on the target system to High Performance.
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 |
#Requires -RunAsAdministrator function Set-HighPerformance { <# .Synopsis Set the power option of a host to High performance. .Description Set the power option of a host to High performance. .Example PS C:\Windows\system32> Set-HighPerformance box1 WARNING: *** Starting WinRM service on box1 Existing Power Schemes (* Active) ----------------------------------- Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) * Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) Power Scheme GUID: db310065-829b-4671-9647-2261c00e86ef (High Performance (ConfigMgr)) Set High performance power option on box1 Existing Power Schemes (* Active) ----------------------------------- Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced) Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (High performance) * Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a (Power saver) Power Scheme GUID: db310065-829b-4671-9647-2261c00e86ef (High Performance (ConfigMgr)) WARNING: *** Stopping WinRM service on box1 .Example get-adcomputer -searchbase ‘OU=workstations,dc=contoso,dc=com’ -filter * -property * | select name | Set-HighPerformance Set-HighPerformance for all the workstation in AD. .Notes Author: Paolo Frigo - https://www.scriptinglibrary.com #> Param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [Alias('name')] [string[]]$ComputerName ) Process { $winRmNotRunning =(Get-Service -ComputerName $ComputerName winrm).Status -eq "Stopped" if ($winRmNotRunning -eq "True"){ Get-Service -ComputerName $ComputerName winrm | Start-Service Write-Warning "*** Starting WinRM service on $ComputerName" } $RemoteSession = New-PSSession -ComputerName $ComputerName #$poweroption = invoke-command -ComputerName $ComputerName -ScriptBlock {powercfg -l} #$hp_guid = $poweroption | %{if($_.contains("High performance")) {$_.split()[3]}} invoke-command -Session $RemoteSession -ScriptBlock { powercfg /l } invoke-command -Session $RemoteSession -ScriptBlock { powercfg /s "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"} Write-Output "Set High performance power option on $ComputerName" invoke-command -Session $RemoteSession -ScriptBlock { powercfg /l } Get-Service -ComputerName $ComputerName winrm | Stop-Service Write-Warning "*** Stopping WinRM service on $ComputerName" Remove-PSSession -Session $RemoteSession } } |
In these scripts, WinRM is required an it will be started remotely if the service is not running and then stopped.
Remove this line if you want to leave it running:
1 |
Get-Service -ComputerName $ComputerName winrm | Stop-Service |
There are many cmdlets that accept ComputerName as an argument. For instance, we can perform remote actions without WinRM service running e.g:
- Get-Process
- Get-Service
- Get-EventLog
- Stop-Computer
- Restart-Computer
- etc.
As usual, you can find these scripts on my GitHub repository.
This doesn’t seem to work on Windows 10 systems
Hi Hank,
Thanks for your comment.
These scripts define a couple of functions, once imported with the dot sourcing and following the instructions they should perform as expected following the examples. I can confirm that these scripts were tested on Win 10, feel free to reach out to me writing a DM attaching your error output.
Please note that I should have added
#requires -runasadministrator
as the first line because to apply these changes it’s required to have high privileges on the endpoint.