{"id":4556,"date":"2012-11-29T00:01:00","date_gmt":"2012-11-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/29\/use-powershell-to-set-the-power-plan-on-networked-servers\/"},"modified":"2012-11-29T00:01:00","modified_gmt":"2012-11-29T00:01:00","slug":"use-powershell-to-set-the-power-plan-on-networked-servers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-set-the-power-plan-on-networked-servers\/","title":{"rendered":"Use PowerShell to Set the Power Plan on Networked Servers"},"content":{"rendered":"<p><strong>Summary:<\/strong> Use a couple of simple Windows PowerShell commands to report the power plan settings on servers as well as setting them.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, today the Scripting Wife and I travel to The Netherlands for the sold-out Dutch Windows PowerShell user group meeting. This all-day user group meeting is sort of like a Dutch version of Windows PowerShell Saturday, except it is on Friday and there is a single track. But oh, what track it is.<\/p>\n<p>Richard Siddaway is speaking about Windows PowerShell and WMI via Lync from the UK; and then I am talking about Windows PowerShell remoting; Jaap Brasser is talking about Splatting; and Jeff Wouters is talking about creating Windows PowerShell tools. Yep, it will be an awesome meeting&mdash;sold out, and it is their FIRST-EVER Windows PowerShell user group meeting. Way to go.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;This is the third article discussing working with Windows PowerShell and WMI to detect and to configure power plans. You should read <a title=\"Use PowerShell and WMI or CIM to View and to Set Power Plans\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/11\/27\/use-powershell-and-wmi-or-cim-to-view-and-to-set-power-plans.aspx\" target=\"_blank\">Use PowerShell and WMI or CIM to View and to Set Power Plans<\/a> for a backgrounder on the technology. You should then read <a title=\"Use PowerShell to Detect Power State and Set Power Plan\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/11\/28\/use-powershell-to-detect-power-state-and-set-power-plan.aspx\" target=\"_blank\">Use PowerShell to Detect Power State and to Set Power Plan<\/a> to see how to use WMI to make the changes. The knowledge in both of those articles is assumed in today&rsquo;s article.<\/p>\n<h2>Detect the power plan on all computers running Windows Server 2012<\/h2>\n<p>To detect the power plan on all of my computers running Windows Server&nbsp;2012, I need to find them. The easiest way to do this is to use the <strong>Get-ADComputer<\/strong> cmdlet from the Active Directory module. The first thing I do is import the Active Directory module, and then obtain the credentials I will use later. These two lines of code appear here.<\/p>\n<p style=\"padding-left: 30px\">Import-Module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential Iammred\\administrator<\/p>\n<p>Because I am not going to use the <strong>OperatingSystem<\/strong> attribute in my script, I do not need to select it via the <strong>Properties<\/strong><em> <\/em>parameter. I only need to use the <strong>OperatingSystem<\/strong> attribute in my filter. Here is the command I use.<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -Filter &#8220;OperatingSystem -like &#8216;* 2012 *'&#8221;<\/p>\n<p>I use the <strong>Name<\/strong><em> <\/em>attribute from the collection of computer objects, and I use the credentials I capture via the <strong>Get-Credential<\/strong> cmdlet. I pass these values to the <strong>New-CimSession<\/strong> cmdlet, and store the returned CIMSession in the <strong>$cim<\/strong> variable. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$cim = New-CimSession -ComputerName $cn.name -Credential $cred&nbsp;<\/p>\n<p>Now, I use the <strong>Get-CimInstance<\/strong> cmdlet to retrieve the active power plan on all of my servers. I pipe the results to the <strong>Format-Table<\/strong> cmdlet to produce a nice output. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance -Name root\\cimv2\\power -Class win32_PowerPlan `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -Filter &#8220;IsActive = &#8216;True'&#8221;&nbsp;&nbsp; -CimSession $cim |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Format-Table PsComputerName, ElementName<\/p>\n<p>The script and the output from the script are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6237.hsg-11-22-12-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6237.hsg-11-22-12-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The complete script is shown here.<\/p>\n<p style=\"padding-left: 30px\">GetServerPowerSaverPlan.ps1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Import-Module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential Iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -Filter &#8220;OperatingSystem -like &#8216;* 2012 *'&#8221;<\/p>\n<p style=\"padding-left: 30px\">$cim = New-CimSession -ComputerName $cn.name -Credential $cred<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Get-CimInstance -Name root\\cimv2\\power -Class win32_PowerPlan `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -Filter &#8220;IsActive = &#8216;True'&#8221;&nbsp;&nbsp; -CimSession $cim |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Format-Table PsComputerName, ElementName<\/p>\n<h2>Setting the plan on all of the computers running Windows Server 2012<\/h2>\n<p>Now I see why my servers appear to be running so slowly following my upgrade&mdash;the active power plan is set to <strong>Balanced<\/strong> on all of the servers. I think I will change them to <strong>High Performance<\/strong>. This works the same way as it did on my laptop the other day&mdash;only now I create a CIM session to all of the remote servers. The <strong>InputObject<\/strong> must be a single instance of the power plan. Because I am dealing with the same type of servers, I cheat a bit, and index into the collection of power plans and simply use the first one. The script works great, but perhaps a better way would be to pipe the results&mdash;but that would slow things down probably. Here is the script.<\/p>\n<p style=\"padding-left: 30px\">SetServerPowerSaverPlan.ps1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Import-Module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential Iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">$cn = Get-ADComputer -Filter &#8220;OperatingSystem -like &#8216;* 2012 *'&#8221;<\/p>\n<p style=\"padding-left: 30px\">$cim = New-CimSession -ComputerName $cn.name -Credential $cred<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$p = Get-CimInstance -Name root\\cimv2\\power -Class win32_PowerPlan `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; -Filter &#8220;ElementName = &#8216;High performance'&#8221;&nbsp; -CimSession $cim<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Invoke-CimMethod -InputObject $p[0] -MethodName Activate -CimSession $cim&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The script returns <strong>True<\/strong> for each successful change. This is shown here.<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReturnValue PSComputerName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True SQL1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Now, I re-run the GetServerPowerSaverPlan script to ensure the changes worked. As shown in the following image, the change is successful.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1007.hsg-11-22-12-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1007.hsg-11-22-12-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Join me tomorrow when I will talk about cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use a couple of simple Windows PowerShell commands to report the power plan settings on servers as well as setting them. Microsoft Scripting Guy, Ed Wilson, is here. Well, today the Scripting Wife and I travel to The Netherlands for the sold-out Dutch Windows PowerShell user group meeting. This all-day user group meeting is [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,173,362,3,45,368],"class_list":["post-4556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-power-management","tag-powershell-3","tag-scripting-guy","tag-windows-powershell","tag-windows-server-2012"],"acf":[],"blog_post_summary":"<p>Summary: Use a couple of simple Windows PowerShell commands to report the power plan settings on servers as well as setting them. Microsoft Scripting Guy, Ed Wilson, is here. Well, today the Scripting Wife and I travel to The Netherlands for the sold-out Dutch Windows PowerShell user group meeting. This all-day user group meeting is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4556"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4556\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}