Friday, September 30, 2011

Executing a PowerShell script from the command line

Actually, this should have been easy. I had a PowerShell script that should be started from a batch file inside a Group Policy Object.

However, starting the script failed on several computers, either because the execution policy was no set or because the user had changed the configuration so PowerShell was waiting for user input when started.

This is command I finally used that could overcome all these problems. It has run successfully on any computer so far.

powershell.exe -NonInteractive -ExecutionPolicy Bypass -file "C:\Script\DoIt.ps1"

Monday, September 5, 2011

Power options in Windows 7: Using PowerCFG.exe

This block post should be a quick round up what I have learned so far about Windows 7 power plan options and using PowerCfg.exe.

Computer does not enter sleep mode

We had problems with several computer that refused to go to Sleep mode and were on 24 hours although the user was away for several hours. This is mostly caused by open files from the network which, by default, prevents Windows from entering sleep mode. Use powercfg –requests to list open files:

requests

My recommendation is to fix this by moving this file locally. However, if you are REALLY REALLY sure what you are doing you can also enforce sleep mode when a program from the network is open.

Getting a report about potential power problems

Using powercfg.exe –energy –output "C:\Energy-Report.html" you can get a detailed report what Windows sees as potential energy problems. This report helped a lot to find outdated drives that caused several problems.

However, keep in mind that not every Error or Warning Windows generates actually needs attention. Use it as a starting point for your investigation.

report

Using scheme_current to apply a setting to the current power plan

In order to change a power setting from the command line, you use powercfg.exe –setACvalueindex/-setDCvalueindex and need to provide four parameters: The power plan GUID you wish to change, the subgroup GUID, the value GUID and the actual value you want to have.

The sub group GUID and the value GUID are always the same, but the power plan GUID can be different on each PC, depending of the settings of the user. However you can use the shortcut scheme_current to apply the setting to the power plan that is activated right now.

A little example might help. Given you wish to change that the Desktop Background Slide show should also run when the computer is running on batteries, you first use powercfg.exe –q to find that setting:

Subgroup GUID: 0d7dbae2-4294-402a-ba8e-26777e8488cd (Desktop background settings)
Power Setting GUID: 309dce9b-bef4-4119-9921-a851fb12f0f4 (Slide show)
Possible Setting Index: 000
Possible Setting Friendly Name: Available
Possible Setting Index: 001
Possible Setting Friendly Name: Paused
Current AC Power Setting Index: 0x00000000
Current DC Power Setting Index: 0x00000001

As you can see the value for “DC” (Battery) is 1, meaning it is “Paused”. Therefore the command need to be in this format:

PowerCfg.exe <Set battery value> <Current power plan GUID> <Subgroup GUID> <Setting GUID> <Set to Available>

The “Available setting” is 0, so the command is now:

PowerCfg.exe <Set battery value> <Current power plan GUID> <Subgroup GUID> <Setting GUID> 0

Setting the battery value (DC Power) is done using –setDCvalueindex:

PowerCfg.exe -setDCvalueindex <Current power plan GUID> <Subgroup GUID> <Setting GUID> 0

The Subgroup GUID is 0d7dbae2-4294-402a-ba8e-26777e8488cd (first line from the output above)

PowerCfg.exe -setDCvalueindex <Current power plan GUID> 0d7dbae2-4294-402a-ba8e-26777e8488cd <Setting GUID> 0

The value GUID is 309dce9b-bef4-4119-9921-a851fb12f0f4 (second line from the output above):

PowerCfg.exe -setDCvalueindex <Current power plan GUID> 0d7dbae2-4294-402a-ba8e-26777e8488cd 309dce9b-bef4-4119-9921-a851fb12f0f4 0

Finally, we need the GUID of the current power plan. The easiest way is to use the shortcut scheme_current, this will automatically apply the setting to the power plan that is currently active.

Powercfg.exe -setDCvalueindex scheme_current 0d7dbae2-4294-402a-ba8e-26777e8488cd 309dce9b-bef4-4119-9921-a851fb12f0f4 0

I might look like a very complicated command when you look at all those GUIDs, but once you have understood the format, it’s easy.

Using –qh paramter to display hidden settings

Powercfg.exe –q will list all settings of the current power plan. However, when you use the paramter –qh, PowerCfg will also display settings that are normally not shown.

hidden

Further reading

If you are interested in even more power setting, check this blog post.