Contents

Youve got the Power!! plan….


Contents

Today I was playing around on some machines on which I noticed the Power Plans were set incorrect, Balanced on a server :'(

Now of course I can do this manually, or I can use PowerShell instead!

Tools not scripts

Since I want to use this more often and want to create my own “toolbelt” [aka module with common tools], I’ve decided to make the solution as advanced functions, not just scripts.

This means that if you simply copy/paste them, you will need to dot source them first in order to use them. Dutch PowerShell MVP Jeff Wouters has a good article on this in case you want some more info on this.

Quick info:

1
Set-Location

to the directory in which you have the .ps1 files in case you have them saved seperately and dot source them using

1
. .\<scriptname>.ps1

The following functions are provided:

  • Get-AllPowerPlan
  • Get-ActivePowerPlan
  • Set-ActivePowerPlan

I’m guessing the names sort speak for themselves, but do take into account that the Set-ActivePowerPlan relies on the other functions to… well… function 🙂

The Code

 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
function Get-AllPowerPlan {
    #Requires -Version 3

    <#

	    .SYNOPSIS
		    Display all available PowerPlans on requested machines.

	    .DESCRIPTION
		    Display all available PowerPlans on requested machines using CIM.

	    .PARAMETER ComputerName
		    ComputerNames to query for PowerPlans.
            Default value is the local computername through $env:COMPUTERNAME

	    .EXAMPLE
		    PS C:\> Get-AllPowerPlan

		    Gets all the PowerPlans for the local machine.

	    .EXAMPLE
		    PS C:\> Get-AllPowerPlan -ComputerName DC01

		    Gets the PowerPlans available for DC01.

	    .NOTES

        Created by: Robert Prüst
        Blog: http://powershellpr0mpt.com
        Version: 1.0

    #>


    [cmdletBinding()]
    param (
        [string[]]$ComputerName =  $env:COMPUTERNAME
    )

    begin{
        Write-Verbose "Starting $($MyInvocation.MyCommand)"
        Write-Verbose "Computer Name is $ComputerName"
    }
    process{
        Get-CimInstance -ClassName Win32_PowerPlan -Namespace root\cimv2\power -ComputerName $ComputerName
    }
}
 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
function Get-ActivePowerPlan {
    #Requires -Version 3

    <#

	    .SYNOPSIS
		    Gets the active PowerPlan on requested machines.

	    .DESCRIPTION
		    Gets the active PowerPlan on requested machines using CIM.

	    .PARAMETER ComputerName
		    ComputerNames to query for PowerPlans.
            Default value is the local computername through $env:COMPUTERNAME

	    .EXAMPLE
		    PS C:\> Get-ActivePowerPlan

		    Gets the active PowerPlan for the local machine.

	    .EXAMPLE
		    PS C:\> Get-ActivePowerPlan -ComputerName DC01

		    Gets the active PowerPlan for DC01.

	    .NOTES

        Created by: Robert Prüst
        Blog: http://powershellpr0mpt.com
        Version: 1.0

    #>


    [cmdletBinding()]
    param (
        [string[]]$ComputerName =  $env:COMPUTERNAME
    )

    begin{
        Write-Verbose "Starting $($MyInvocation.MyCommand)"
        Write-Verbose "Computer Name is $ComputerName"
    }
    process{
        Get-CimInstance -ClassName Win32_PowerPlan -Namespace root\cimv2\power -ComputerName $ComputerName -Filter "IsActive = 'True'"
    }
}
 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
function Set-ActivePowerPlan {
    #Requires -Version 3

    <#

	    .SYNOPSIS
		    Sets the active PowerPlan on requested machines.

	    .DESCRIPTION
		    Sets the active PowerPlan on requested machines using CIM.

	    .PARAMETER ComputerName
		    ComputerNames to query for PowerPlans.
            Default value is the local computername through $env:COMPUTERNAME

	    .EXAMPLE
		    PS C:\> Set-ActivePowerPlan -Plan 'Balanced'

		    Sets the active PowerPlan for the local machine.

	    .EXAMPLE
		    PS C:\> Set-ActivePowerPlan -ComputerName DC01

		    Sets the active PowerPlan for DC01.

	    .NOTES

        Created by: Robert Prüst
        Blog: http://powershellpr0mpt.com
        Version: 1.0

    #>


    [cmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName =  $env:COMPUTERNAME,
        [Parameter(Position=1)]
        $Plan = (Get-AllPowerPlan  | Select-Object ElementName,IsActive | Out-GridView -PassThru -Title 'Select the PowerPlan you wish to have active')
        )

    begin{
        Write-Verbose "Starting $($MyInvocation.MyCommand)"
        Write-Verbose "Computer Name is $ComputerName"

        $PlanName = ($Plan).ElementName
            Write-Verbose "Plan Name is $PlanName"
    }
    process{
        foreach ($Computer in $ComputerName){
            $curPPlan = (Get-ActivePowerPlan -ComputerName $Computer).ElementName
            Write-Verbose "Current active plan is $curPPlan"

            if ($PlanName -ne $curPPlan){
                try {
                    $PPlan = Get-CimInstance -ClassName Win32_PowerPlan -Namespace root\cimv2\power -ComputerName $Computer -Filter "ElementName = '$PlanName'" -ErrorAction Stop
                } catch {
                    Write-Error "Unable to find a Power Plan with the name of $PlanName on computer $Computer"
                    throw
                }
                if ($PPlan) {
                    Invoke-CimMethod -Computer $Computer -InputObject $PPlan -MethodName Activate
                }
                $newPPlan = (Get-ActivePowerPlan).ElementName
                Write-Verbose "Current active plan is $newPPlan on computer $Computer"
            } else {
                Write-Warning "Power Plan already set to $PlanName on computer $Computer"
            }
        }
    }
}

Happy Scripting! 🙂