ForeverTheOtherGuy66

Winget_Auto_Update.bat

Jun 11th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.09 KB | Source Code | 0 0
  1. @echo off
  2. setlocal enabledelayedexpansion
  3.  
  4. REM === Configuration ===
  5. set "SafePath=C:\ProgramData\Winget_Auto_Update.bat"
  6. set "LogFile=C:\ProgramData\Winget_Auto_Update.log"
  7.  
  8. REM === Normalize running path vs safe path ===
  9. call :Normalize "%~f0" CurrPath
  10. call :Normalize "%SafePath%" DestPath
  11.  
  12. if /I not "%CurrPath%"=="%DestPath%" (
  13.     echo Installing script to %SafePath% and scheduling task...
  14.     copy "%~f0" "%SafePath%" /Y >nul
  15.     schtasks /Create /TN "Winget Auto-Update" ^
  16.         /TR "\"%SafePath%\"" ^
  17.         /SC ONSTART ^
  18.         /RL HIGHEST ^
  19.         /RU SYSTEM ^
  20.         /F
  21.     echo Installation complete. Reboot to let the task run automatically.
  22.     exit /B
  23. )
  24.  
  25. REM === We’re running as SYSTEM from %SafePath% ===
  26. >>"%LogFile%" echo.
  27. >>"%LogFile%" echo ====================================================
  28. >>"%LogFile%" echo RUN AT %DATE% %TIME%
  29. >>"%LogFile%" echo Locating winget.exe...
  30.  
  31. REM 1) Try WindowsApps (UWP install)
  32. set "WinAppDir=%ProgramFiles%\WindowsApps"
  33. set "Found="
  34. for /f "delims=" %%D in ('dir "%WinAppDir%\Microsoft.DesktopAppInstaller_*" /b /ad ^| sort /R') do (
  35.     if exist "%WinAppDir%\%%D\winget.exe" (
  36.         set "WingetExe=%WinAppDir%\%%D\winget.exe"
  37.         set "Found=1"
  38.         goto :FoundWinget
  39.     )
  40. )
  41. :FoundWinget
  42.  
  43. REM 2) Fallback to Sysnative/System32
  44. if not defined Found (
  45.     if exist "%windir%\Sysnative\winget.exe" (
  46.         set "WingetExe=%windir%\Sysnative\winget.exe"
  47.     ) else (
  48.         set "WingetExe=%windir%\System32\winget.exe"
  49.     )
  50. )
  51.  
  52. >>"%LogFile%" echo Using: %WingetExe%
  53.  
  54. if not exist "%WingetExe%" (
  55.     >>"%LogFile%" echo ERROR: winget.exe not found at "%WingetExe%"
  56.     exit /B 1
  57. )
  58.  
  59. >>"%LogFile%" echo Waiting 3 minutes before updating...
  60. timeout /t 180 /nobreak >>"%LogFile%" 2>&1
  61.  
  62. >>"%LogFile%" echo Starting winget upgrade...
  63. "%WingetExe%" upgrade --all --accept-package-agreements --accept-source-agreements >>"%LogFile%" 2>&1
  64.  
  65. >>"%LogFile%" echo Completed at %DATE% %TIME%
  66. exit /B
  67.  
  68. :Normalize
  69. set "p=%~1"
  70. if "%p:~-1%"=="\" set "p=%p:~0,-1%"
  71. for %%A in ("%p%") do set "%2=%%~fA"
  72. exit /B
  73.  
Advertisement
Add Comment
Please, Sign In to add comment