My (humble) contribution

Discussion in 'Windows 10' started by Thomas Dubreuil, Dec 24, 2018.

  1. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    357
    594
    10
    I started scripting few monthes ago and nowhere close as some of the talents we are lucky to have here.
    They are a such a great source to learn (and steal code parts :D )
    So sharing my work is a way to thanks the community for all these awesomeness!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. rayleigh_otter

    rayleigh_otter MDL Expert

    Aug 8, 2018
    1,121
    927
    60
    #22 rayleigh_otter, Jun 8, 2019
    Last edited: Jun 8, 2019
    My bats are very simple, i only know a lot about bat basics, i usually use RegFromApp to harvest from a tweaker program, convert with Reg Convert then edit to suit. You have joined @TairikuOkami as my source of good bat stuff. :worthy:
     
  3. fLOW.

    fLOW. MDL Senior Member

    Jul 28, 2009
    426
    398
    10
    The "PowerManagementUSB" only works on "Universal Serial Bus Controllers" in Device Manager but there's also other entries in "Human Interface Devices" which contain other USB devices with the Power Management option.

    Is it possible to disable those via another script?
     
  4. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    357
    594
    10
    #24 Thomas Dubreuil, Jun 13, 2019
    Last edited: Jun 13, 2019
    (OP)
    mmm yes it is...;)
    The two power management PS scripts were initially made for "Optimize NextGen" , just modified them a bit (very) recently, to be able to share for people using outside the optimization script.

    I added a HID part (made in pure batch) in the Optimization script...in which you can run only "Power Management" part if you like.
    It's a bit complicated to make one now or soon, but if you or someone want to make that script for us :) here is the part involved :
    Code:
        setlocal EnableExtensions DisableDelayedExpansion
        set "DetectionCount=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendOn" /t REG_DWORD') do call :ProcessLine "%%i"
        if not %DetectionCount% == 0 endlocal & ( goto :SelectiveSuspend_part2)
    
    :ProcessLine
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendOn" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A DetectionCount+=1
        goto :eof
    
    :SelectiveSuspend_part2
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection2_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "EnableSelectiveSuspend" /t REG_DWORD') do call :ProcessLine2 "%%i"
        if not %Detection2_Count% == 0 endlocal & ( goto :SelectiveSuspend_part3)
    
    :ProcessLine2
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "EnableSelectiveSuspend" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection2_Count+=1
        goto :eof
    
    :SelectiveSuspend_part3
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection3_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendEnabled" /t REG_DWORD') do call :ProcessLine3 "%%i"
        if not %Detection3_Count% == 0 endlocal & ( goto :SelectiveSuspend_part4)
    
    :ProcessLine3
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendEnabled" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection3_Count+=1
        goto :eof
    
    :SelectiveSuspend_part4
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection4_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendEnabled" /t REG_BINARY') do call :ProcessLine4 "%%i"
        if not %Detection4_Count% == 0 endlocal & ( goto :SelectiveSuspend_part5)
    
    :ProcessLine4
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendEnabled" /t REG_BINARY /d "00" /f >NUL 2>&1
        set /A Detection4_Count+=1
        goto :eof
    
    :SelectiveSuspend_part5
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection5_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "DeviceSelectiveSuspended" /t REG_DWORD') do call :ProcessLine5 "%%i"
        if not %Detection5_Count% == 0 echo Done.& endlocal & ( goto :SelectiveSuspend_Scripts)
    
    :ProcessLine5
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "DeviceSelectiveSuspended" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection5_Count+=1
        goto :eof
    
    :SelectiveSuspend_Scripts
    ...
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    357
    594
    10
    work better than what? I don't do C# (yet :D) have you tried?
    you just asked me if it was possible via another script, I said might be, and even gave the bit of code...Not sure what I can do more o_O
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. fLOW.

    fLOW. MDL Senior Member

    Jul 28, 2009
    426
    398
    10
    Sure, i appreciate your help. I was just digging around to see if it was possible to do the same thing for other USB devices (in this case the HID Devices) while still using Powershell, to have them all in one script.

    I don't understand much of this so i asked :D
     
  7. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    357
    594
    10
    #28 Thomas Dubreuil, Jun 13, 2019
    Last edited: Jul 9, 2019
    (OP)
    yeah...will try to have a look when possible ;)
    I remember it was quite a fight this power management part...
    The HID script alone took me very long, but was very good way to master the :call function (thanks to S1ave77 for guiding me in the good direction)

    Anyway, you know you can call a batch script from powershell...or powershell script from batch?
    Ok it makes 2-3 files, but called from 1 script. :)

    If you reall want 1 file only...go with "Optimize_NextGen.exe"...
    Type 1 (optimize) then 5 (apply powermanagement only)...Done

    Or...copy that script part above and add command to launch PS scripts...It's not so hard, something like:

    Code:
    @echo off
    
    %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>NUL 2>NUL || goto :NOADMIN
    
    :: Disable "allow the computer to turn off this device to save power" for HID Devices under PowerManagement tab in Device Manager
        <nul set /p dummyName=Disabling "Allow the computer to turn off this device to save power" for HID Devices under Power Management tab in Device Manager:
        setlocal EnableExtensions DisableDelayedExpansion
        set "DetectionCount=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendOn" /t REG_DWORD') do call :ProcessLine "%%i"
        if not %DetectionCount% == 0 endlocal & ( goto :SelectiveSuspend_part2)
    
    :ProcessLine
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendOn" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A DetectionCount+=1
        goto :eof
    
    :SelectiveSuspend_part2
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection2_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "EnableSelectiveSuspend" /t REG_DWORD') do call :ProcessLine2 "%%i"
        if not %Detection2_Count% == 0 endlocal & ( goto :SelectiveSuspend_part3)
    
    :ProcessLine2
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "EnableSelectiveSuspend" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection2_Count+=1
        goto :eof
    
    :SelectiveSuspend_part3
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection3_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendEnabled" /t REG_DWORD') do call :ProcessLine3 "%%i"
        if not %Detection3_Count% == 0 endlocal & ( goto :SelectiveSuspend_part4)
    
    :ProcessLine3
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendEnabled" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection3_Count+=1
        goto :eof
    
    :SelectiveSuspend_part4
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection4_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "SelectiveSuspendEnabled" /t REG_BINARY') do call :ProcessLine4 "%%i"
        if not %Detection4_Count% == 0 endlocal & ( goto :SelectiveSuspend_part5)
    
    :ProcessLine4
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "SelectiveSuspendEnabled" /t REG_BINARY /d "00" /f >NUL 2>&1
        set /A Detection4_Count+=1
        goto :eof
    
    :SelectiveSuspend_part5
        setlocal EnableExtensions DisableDelayedExpansion
        set "Detection5_Count=0"
        for /f "delims=" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB" /s /v "DeviceSelectiveSuspended" /t REG_DWORD') do call :ProcessLine5 "%%i"
        if not %Detection5_Count% == 0 echo Done.& endlocal & ( goto :SelectiveSuspend_Scripts)
    
    :ProcessLine5
        set "RegistryLine=%~1"
        if "%RegistryLine:~0,5%" == "HKEY_" set "RegistryKey=%~1" & goto :eof
        reg add "%RegistryKey%" /v "DeviceSelectiveSuspended" /t REG_DWORD /d 0 /f >NUL 2>&1
        set /A Detection5_Count+=1
        goto :eof
    
    :SelectiveSuspend_Scripts
    :: Disable "allow the computer to turn off this device to save power" for USBHub under PowerManagement tab in Device Manager
        <nul set /p dummyName=Disabling "Allow the computer to turn off this device to save power" for USB Hubs under Power Management tab in Device Manager:
        PowerShell -NoProfile -ExecutionPolicy Bypass -File "%~dp0PowerManagementUSB.ps1" >NUL 2>&1
        echo Done.
    
    :: Disable "allow the computer to turn off this device to save power" for Network Adapters under PowerManagement tab in Device Manager
       <nul set /p dummyName=Disabling "Allow the computer to turn off this device to save power" for Network Adapters under Power Management tab in Device Manager:
        PowerShell -NoProfile -ExecutionPolicy Bypass -File "%~dp0PowerManagementNIC.ps1" >NUL 2>&1
        echo Done.
        echo :
        echo Please reboot the machine for the changes to take effect.
    
    timeout /t 2 /nobreak >NUL 2>&1
    exit /b
    
    ::============================================================================================================
    :NOADMIN
    ::============================================================================================================
        echo You must have administrator rights to run this script.
        <nul set /p dummyName=Press any key to exit...
        pause >nul
        goto :eof
    
    You won't have the fancy colors and displayed infos like in OptimizeNG, that's all ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. rayleigh_otter

    rayleigh_otter MDL Expert

    Aug 8, 2018
    1,121
    927
    60
    White on blue, Color 1F is my favourite :)
     
  9. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    357
    594
    10
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. rayleigh_otter

    rayleigh_otter MDL Expert

    Aug 8, 2018
    1,121
    927
    60
    Had a look at the link, my brain farted and left me flying solo, it might aswell be all in Klingon :biggrin3: