How to automate windows update using powershell and batch script?
Create MSUpdates.log file in D: drive
Create installpatchexecute.cmd in D: drive with this content:
pushd D:
powershell.exe .\installpatch.ps1
if %errorlevel% neq 0 goto error
popd
goto end
:error
echo "An error occurred" >> D:\MSUpdates.log
exit /b 1
:end
exit /b 0
Create installpatch.ps1 file in D: drive with this content:
netsh winhttp set proxy "xxxxxx:80" #(proxy if any)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot | Out-File "D:\MSUpdates.log" -Force
Create Schedule task to run on every monday at 6pm and relax :)
pushd c:\windows\system32
SCHTASKS /Create /S localhost /RU SYSTEM /SC WEEKLY /D MON /ST 18:00 /TN "InstallWindowsPatch" /TR "D:\installpatchexecute.cmd"