How do I remove (successfully) a Power Scheme before a Sysprep?

Discussion in 'Scripting' started by Flipp3r, Aug 4, 2021.

  1. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,965
    908
    60
    Hi,
    I normally prepare my images in Audit mode but have some software that needs to be setup as an end user.
    My clean install of Win10 logs me in as Administrator. Since I don't want the system to Sleep, I've created a Power Scheme that prevents this. ALL_ON_POWER_SCHEME.cmd:
    Code:
    @echo off
    SetLocal
    
    :: Get GUID of current power scheme
    FOR /F "tokens=4" %%G IN ('powercfg /getactivescheme') DO set activeschemeGUID=%%G
    
    :: Custom Power Scheme Name
    set custom_name=ALL_ON_POWER_SCHEME
    
    :: Copy current active scheme and get GUID of that copy
    FOR /F "tokens=4" %%G IN ('powercfg -DUPLICATESCHEME %activeschemeGUID%') DO set custom_GUID=%%G
    
    :: Change the name of the new scheme (the copy) to the custom name
    powercfg -CHANGENAME %custom_GUID% %custom_name%
    
    :SetCustomActive
    powercfg -SETACTIVE %custom_GUID%
    set activeschemeGUID=%custom_GUID%
    
    POWERCFG /CHANGE monitor-timeout-ac 0
    POWERCFG /CHANGE disk-timeout-ac 0
    POWERCFG /CHANGE standby-timeout-ac 0
    POWERCFG /CHANGE hibernate-timeout-ac 0
    
    EndLocal
    exit /b 0
    
    -
    Once I have installed everything, I want to restore the Power Scheme back to Balanced, remove the created ALL_ON_POWER_SCHEME and Sysprep the system. In my Sysprep.cmd I do the above, even giving it a reboot. On reboot the Power Scheme is set to Balanced and ALL_ON_POWER_SCHEME is removed.
    When I sysprep & log in as a normal user, ALL_ON_POWER_SCHEME is set.
    Code:
    @echo off
    :: ===== Set Power Scheme to Default (Balanced) if not in Audit Mode =====================================================================
    set custom_name=ALL_ON_POWER_SCHEME
    :: Check if Custom Plan exists, get its GUID
    FOR /F "tokens=4" %%G IN ('powercfg -list ^| find "%custom_name%"') DO (
        REM custom power scheme with that name already exists
        set custom_GUID=%%G
        goto GotCustom
    )
    goto Done
    
    :GotCustom
    :: Set Balanced Power Scheme
    POWERCFG /SETACTIVE 381b4222-f694-41f0-9685-ff5bb260df2e
    
    :: Delete Custom Plan
    POWERCFG /DELETE %custom_GUID%
    
    SHUTDOWN -R -F -T 10 /C "System will now Reboot, please re-run Sysprep.cmd"
    exit /b 0
    
    :Done
    start /wait Powercfg -restoredefaultschemes
    start /wait c:\windows\System32\Sysprep\Sysprep.exe /oobe /quit /unattend:c:\windows\System32\Sysprep\sysprep.xml
    (goto) 2>nul & del "%~f0" & shutdown -r -f -t 10
    What's going on? How do I get rid of this?
    Thanks!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,965
    908
    60
    I've also tried
    before Sysprep.
    I've also tried adding that to my FirstLogon.cmd file as well.
    This is driving me nuts!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,219
    84,891
    340
    Does it still in registry?
    Code:
    reg query HKLM\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
    reg query HKLM\SYSTEM\ControlSet002\Control\Power\User\PowerSchemes
     
  4. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    used to add something like this to uupdump conversion script for high performance power profile on a potato:
    Code:
    ::script.bat begin
    @echo off
    
    :: enable high performance
    call :power 1
    
    ::main stuff begin
    
    ::main stuff end
    
    :: revert to previous (by default balanced)
    call :power 0
    
    exit/b
    ::functions section
    
    :power USAGE:    call :power 1  ;  call :power 0
    if 1%1 equ 10 (powercfg /s %h\p% & exit/b) else for /f "tokens=2 delims=:(" %%s in ('powercfg /getactivescheme') do set h\p=%%s
    powercfg /s SCHEME_MIN & exit/b AveYo: toggle high performance
    
    the important thing being to revert the scheme before exiting / terminating the script that set it
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,965
    908
    60
    Info while setting up:
    Code:
    C:\Windows\system32>powercfg /l
    
    Existing Power Schemes (* Active)
    -----------------------------------
    Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
    Power Scheme GUID: c129a3ef-d5b5-49f6-b2de-9dbd59ab50f9  (ALL_ON_POWER_SCHEME) *
    
    C:\Windows\system32>reg query HKLM\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
    
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
        ActivePowerScheme    REG_SZ    c129a3ef-d5b5-49f6-b2de-9dbd59ab50f9
    
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\381b4222-f694-41f0-9685-ff5bb260df2e
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\3af9B8d9-7c97-431d-ad78-34a8bfea439f
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\961cc777-2547-4f9d-8174-7d86181b8a7a
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\a1841308-3541-4fab-bc81-f71556f20b4a
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\c129a3ef-d5b5-49f6-b2de-9dbd59ab50f9
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\ded574b5-45a0-4f42-8737-46345c09c238
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\e9a42b02-d5df-448d-aa00-03f14749eb61
    
    C:\Windows\system32>reg query HKLM\SYSTEM\ControlSet002\Control\Power\User\PowerSchemes
    ERROR: The system was unable to find the specified registry key or value.
    Setting Balanced Scheme, removing ALL_ON_POWER_SCHEME, info after a reboot:
    Code:
    C:\Windows\system32>powercfg /l
    
    Existing Power Schemes (* Active)
    -----------------------------------
    Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced) *
    
    C:\Windows\system32>reg query HKLM\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
    
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes
        ActivePowerScheme    REG_SZ    381b4222-f694-41f0-9685-ff5bb260df2e
    
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\381b4222-f694-41f0-9685-ff5bb260df2e
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\3af9B8d9-7c97-431d-ad78-34a8bfea439f
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\961cc777-2547-4f9d-8174-7d86181b8a7a
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\a1841308-3541-4fab-bc81-f71556f20b4a
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\ded574b5-45a0-4f42-8737-46345c09c238
    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Power\User\PowerSchemes\e9a42b02-d5df-448d-aa00-03f14749eb61
    
    C:\Windows\system32>reg query HKLM\SYSTEM\ControlSet002\Control\Power\User\PowerSchemes
    ERROR: The system was unable to find the specified registry key or value.
    LOOKS GOOD. SHOULD BE FINE YOU'D THINK!

    After Sysprep, Info at OOBE:
    [​IMG]
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,965
    908
    60
    Thanks @BAU
    Problem is there's a lot of rebooting involved during install. I often walk away to do other tasks & didn't want it to sleep.
    Actually one of our tests is running rebooter to make sure the ssd get's initialised every time.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    I had a feeling there's rebooting involved - microsoft warns about it as it leads to unpredictable behavior
    You need to get rid of / streamline that somehow - use devcon? alter registry in one go? (this includes powercfg) probably tedious to check all your scripts but should be done
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,219
    84,891
    340
    Yeah, registry changes to HKLM\SYSTEM should be persistent, i wonder how it get restored
    is Last Known Good Configuration active?

    you could do the registry changes manually (delete custom profile key, and set ActivePowerScheme value)
    the key is system protected, you need NSudo or similar
    try that before sysprep, and see if it stick