Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal enabledelayedexpansion
- REM === Configuration ===
- set "SafePath=C:\ProgramData\Winget_Auto_Update.bat"
- set "LogFile=C:\ProgramData\Winget_Auto_Update.log"
- REM === Normalize running path vs safe path ===
- call :Normalize "%~f0" CurrPath
- call :Normalize "%SafePath%" DestPath
- if /I not "%CurrPath%"=="%DestPath%" (
- echo Installing script to %SafePath% and scheduling task...
- copy "%~f0" "%SafePath%" /Y >nul
- schtasks /Create /TN "Winget Auto-Update" ^
- /TR "\"%SafePath%\"" ^
- /SC ONSTART ^
- /RL HIGHEST ^
- /RU SYSTEM ^
- /F
- echo Installation complete. Reboot to let the task run automatically.
- exit /B
- )
- REM === We’re running as SYSTEM from %SafePath% ===
- >>"%LogFile%" echo.
- >>"%LogFile%" echo ====================================================
- >>"%LogFile%" echo RUN AT %DATE% %TIME%
- >>"%LogFile%" echo Locating winget.exe...
- REM 1) Try WindowsApps (UWP install)
- set "WinAppDir=%ProgramFiles%\WindowsApps"
- set "Found="
- for /f "delims=" %%D in ('dir "%WinAppDir%\Microsoft.DesktopAppInstaller_*" /b /ad ^| sort /R') do (
- if exist "%WinAppDir%\%%D\winget.exe" (
- set "WingetExe=%WinAppDir%\%%D\winget.exe"
- set "Found=1"
- goto :FoundWinget
- )
- )
- :FoundWinget
- REM 2) Fallback to Sysnative/System32
- if not defined Found (
- if exist "%windir%\Sysnative\winget.exe" (
- set "WingetExe=%windir%\Sysnative\winget.exe"
- ) else (
- set "WingetExe=%windir%\System32\winget.exe"
- )
- )
- >>"%LogFile%" echo Using: %WingetExe%
- if not exist "%WingetExe%" (
- >>"%LogFile%" echo ERROR: winget.exe not found at "%WingetExe%"
- exit /B 1
- )
- >>"%LogFile%" echo Waiting 3 minutes before updating...
- timeout /t 180 /nobreak >>"%LogFile%" 2>&1
- >>"%LogFile%" echo Starting winget upgrade...
- "%WingetExe%" upgrade --all --accept-package-agreements --accept-source-agreements >>"%LogFile%" 2>&1
- >>"%LogFile%" echo Completed at %DATE% %TIME%
- exit /B
- :Normalize
- set "p=%~1"
- if "%p:~-1%"=="\" set "p=%p:~0,-1%"
- for %%A in ("%p%") do set "%2=%%~fA"
- exit /B
Advertisement
Add Comment
Please, Sign In to add comment