Search code examples
powershellbatch-fileregistryjscripthta

Network shared batch file wont change registry


I have following batch file:

powershell -Command "& {Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate -Name AutoDownload -Value 2 -Type DWord}"

If I run it locally it does make necessary changes to the registry, however if I upload it on a shared folder and execute this batch file via a shared folder it won't change the registry.

What am I missing here exactly? It works fine if file is located on my local computer, but if I will launch it from a shared folder (without downloading locally) no changes are made.

The batch file is called via HTA file with the following code:

function Win10UninstallUnnecessaryApps(){
    var shell = new ActiveXObject("WScript.Shell");
    var path = '"file:\\\\fs\\FIle Share\\SA Support\\ZverTools\\Win10UninstallUnnecessaryApps.bat"'; 
    shell.run(path,1,false); 
}

Solution

  • I know the following method is not perfect, but it was the only solution that worked for my case. This is some sort of hack method to perform registry changes executed by the HTA application. You should create a batch file that downloads your another batch file (which has registry changing code inside), after that select that specific file and use the SendKeys method to mimic click event. As soon as I updated my HTA application with the following hack, it worked well and I got my single click registry changes after all.

    Here is starting batch file (executed from HTA application button) code which runs another batch file (the one which should edit registry values):

    @if (@CodeSection == @Batch) @then
    
    @echo off
    
    set SendKeys=CScript //nologo //E:JScript "%~F0"
    
    xcopy "\\fs\FIle Share\SA Support\ZverTools\Win10UninstallUnnecessaryApps.bat" "%USERPROFILE%" /y
    
    TIMEOUT /T 2 /NOBREAK
    
    set targetfilepath=%USERPROFILE%\Win10UninstallUnnecessaryApps.bat   
    explorer.exe /select, "%TARGETFILEPATH%"
    
    TIMEOUT /T 4 /NOBREAK
    
    %SendKeys% "{ENTER}"
    
    goto :EOF
    
    @end
    
    var WshShell = WScript.CreateObject("WScript.Shell");
    WshShell.SendKeys(WScript.Arguments(0));
    

    This was the only solution that allowed me to change registry values with a single HTA application button click.

    Registry value which I wanted to change via batch file (executed from HTA app) was:

    powershell -Command "& {Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsStore\WindowsUpdate -Name AutoDownload -Value 2 -Type DWord}"