Author Posts

August 8, 2018 at 7:33 pm

Hello POSH fam,

Just wondering if someone could provide some guidance on how I can parse a result of a powercfg /list cmdlet. What I hope to accomplish is that to look for a particular string and if it matches to remove the GUID that contains that string.

For example:

powercfg /list results in the following:

Existing Power Schemes (* Active)
———————————–
Power Scheme GUID: 2a6575c5-f176-4d98-a67b-6ed72b8e4642Ā  (Disable_PC_Lock)
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2eĀ  (Balanced) *

I would like to know how I can direct Powershell to locate and remove the GUID containing "*Disable_PC_Lock*"

I ran a GM on powercfg /list but I don't seem to be getting the information I am looking for; moreover I'm not knowledgeable enough to know what I'm looking for šŸ˜›

When I tried to use Export-CSV I am getting nothing by nothing but a Column named "Length" and a bunch of numbers below under the same column.


#Save all powerplans into txt file

powercfg /list >> D:\POC\plan.txt

#Assign results to variable

$plan = Get-Content D:\POC\plan.txt

#Loop and locate power plan

$plan | ForEach-Object {if ($plan -like "(*Disable_PC_Lock*)"){'Yes it does'}else{'No
it does not'}}

Consequently I will be replacing the text when a condition is met with a powercfg -delete -GUID ###

I'm not sure if I explained my purpose correctly, but please let me know if I need to be more clear.

Thanks in advance,

Alex

 

August 8, 2018 at 8:37 pm

It's a good deal easier to grab the power plan GUID through CIM, rather than parsing some nasty text output. Sadly, CIM doesn't give us a delete method, so we'll pass the GUID back along to powercfg.

# Grabs a power plan named 'My Custom Plan 1'
$PowerPlan = Get-CimInstance -Namespace root\cimv2\power -ClassName win32_powerplan -Filter "ElementName = 'My Custom Plan 1'"

# Our GUID is stored in the InstanceId property as a string 
# "Microsoft:PowerPlan\{066ed56a-25b5-4c93-9af1-bdb5caa51883}"
# We'll split on the first curly brace and take the second element from the resulting array
$Guid = $PowerPlan.InstanceId.split('{')[1]

# The $Guid variable now contains "066ed56a-25b5-4c93-9af1-bdb5caa51883}"
# Let's drop that last brace.

$Guid = $Guid -replace "}", ""

# Now we can pass that along to powercfg for the deletion
powercfg /D $Guid

August 8, 2018 at 8:40 pm

 

How about:

if ($plan | Select-String 'Disable_PC_Lock') {

Do Something

}

August 9, 2018 at 3:59 am

@alex, I would suggest you to go with CIM and Powercfg combination as suggested by mark. When using PowerShell, we should use its the Power of structured data(objects) which makes our life easy.

August 9, 2018 at 2:33 pm

That's unfortunate that you can't delete the wmi object. I was also hoping for a powercfg alias for the custom plan (SCHEME_MAX, SCHEME_MIN, SCHEME_BALANCED). And there isn't a wmi property with the guid by itself.

Get-CimInstance win32_powerplan -Namespace root\cimv2\power | 
  where elementname -eq 'my custom plan 1' | Remove-CimInstance

Remove-CimInstance : Provider is not capable of the attempted operation
At line:1 char:107
+ ... power | where elementname -eq 'my custom plan 1' | Remove-CimInstance
+                                                        ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Win32_PowerPlan...c-5d1b-48d5...):CimInstance) [Remove-CimInstance], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041024,Microsoft.Management.Infrastructure.CimCmdlets.RemoveCimInstanceCommand

August 14, 2018 at 7:08 pm

Hi POSH fam,

Sorry for the late reply.

I truly appreciate all of the feedback and guidance. I have gone ahead and created a script via the "Get-CimInstance" cmdlet and I am now able to achieve my goal of locating and removing via Powershell and powercfg.

I learned the process/method of manipulating data in parsing. šŸ™‚

Thanks again and much appreciated.