Skip to content

Instantly share code, notes, and snippets.

@bgelens
Last active October 5, 2016 18:57
Show Gist options
  • Save bgelens/127ef940c6cacfeda88a25021ac0caad to your computer and use it in GitHub Desktop.
Save bgelens/127ef940c6cacfeda88a25021ac0caad to your computer and use it in GitHub Desktop.
#requires -Version 3
function Get-PowerPlan {
<#
.SYNOPSIS
Display PowerPlans on requested machines.
.DESCRIPTION
Display PowerPlans on requested machines using CIM.
.PARAMETER ComputerName
ComputerNames to query for PowerPlans.
Default value is the local computername through $env:COMPUTERNAME
.PARAMETER Current
Only displays currently active Powerplan.
.EXAMPLE
PS C:\> Get-PowerPlan
Gets all the PowerPlans for the local machine.
.EXAMPLE
PS C:\> Get-PowerPlan -ComputerName DC01
Gets the PowerPlans available for DC01.
.EXAMPLE
PS C:\> Get-Content -Path .\computers.txt | Get-PowerPlan -Current
Gets the current active PowerPlan on all computers defined in the computers.txt file.
.NOTES
Created by: Robert Prüst
Blog: http://powershellpr0mpt.com
Version: 1.0
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string[]] $ComputerName = $env:COMPUTERNAME,
[switch] $Current
)
process{
foreach ($C in $ComputerName) {
try {
$CimSession = New-CimSession -ComputerName $C
$InstanceArgs = @{
ClassName = 'Win32_PowerPlan'
Namespace = 'root\cimv2\power'
CimSession = $CimSession
}
if ($Current) {
[void] $InstanceArgs.Add('Filter',"IsActive = 'True'")
}
Get-CimInstance @InstanceArgs
} catch {
Write-Error -ErrorRecord $_
} finally {
if ($null -ne $CimSession) {
$CimSession | Remove-CimSession
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment