Batch Command for Windows 10 Updates for Scheduling

0

I'm looking to run windows update (check and download updates) on Windows 10 machine on a certain day and time; let's say Saturday at 10pm.

Is there a batch command that does this? I can then use Task Scheduler to run that task but I cannot find any documentation on either a batch command for this.

Eric Nguyen

Posted 2019-06-10T19:24:39.470

Reputation: 121

Answers

0

Sounds like you need the powershell module PSWindowsUpdate.

  1. Open elevated powershell (you may need to set Set-ExecutionPolicy RemoteSigned y)

  2. Windows 10 has powershell version 5 or above.

    $PSVersionTable.PSVersion
    
  3. Get the PSWindowsUpdate module (push y at all prompts)

     Install-Module PSWindowsUpdate
    
  4. List each available PSWindowsUpdate cmdlet with:

     Get-Command -Module PSWindowsUpdate
    
  5. Get all microsoft updates:

     Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d
    
  6. The basic script to run to update is:

    Get-WUInstall –MicrosoftUpdate –AcceptAll –AutoReboot
    
  7. Now all you need to do is create a batch script to run the powershell script. The one I us on some older machines is as follows:

C:\TempCfg\winupdate_execution_policy_bypass.bat

@echo on
echo Loading Powershell with Correct Policy...
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\TempCfg\winupdate_powershell_script.ps1""' -Verb RunAs}"
echo waiting for 90 seconds before exit
timeout /t 90
exit

C:\TempCfg\winupdate_powershell_script.ps1 (This also shows, how I have exported various commands to text files for easy reveiw.)

Write-Verbose ‘Windows Update’ -verbose
Import-Module PSWindowsUpdate -verbose
Invoke-Command  {Get-Date -Format G -verbose | Out-File -FilePath C:\TempCfg\logs\winlog1.txt -Append -width 300}
Invoke-Command  {Get-WUServiceManager -verbose | Out-File -FilePath C:\TempCfg\logs\winlog1.txt -Append -width 300}
Invoke-Command  {Get-Date -Format G -verbose | Out-File -FilePath C:\TempCfg\logs\winlog2.txt -Append -width 300}
Invoke-Command  {Get-WURebootStatus -verbose | Out-File -FilePath C:\TempCfg\logs\winlog2.txt -Append -width 300}
Invoke-Command  {Get-Date -Format G -verbose | Out-File -FilePath C:\TempCfg\logs\winlog3.txt -Append -width 300}
Invoke-Command  {Get-WUInstallerStatus -verbose | Out-File -FilePath C:\TempCfg\logs\winlog3.txt -Append -width 300}
Invoke-Command  {Get-Date -Format G -verbose | Out-File -FilePath C:\TempCfg\logs\winlog5.txt -Append -width 300}
Import-Module PSWindowsUpdate -verbose
Write-Verbose ‘Invoking command Get-WUInstall -MicrosoftUpdate -IgnoreUserInput -AcceptAll -AutoReboot -Verbose’ -verbose
Invoke-Command  {Get-WUInstall  -AcceptAll -AutoReboot -Confirm:$FALSE -verbose | Out-File -FilePath C:\TempCfg\logs\winlog5.txt -Append -width 300}

For example, I have set it up in task scheduler as admin C:\TempCfg\winupdate_execution_policy_bypass.bat

  1. Then you just need to find a way to turn off the windows updates, such as the group policy editor, or third party programs. You will need to have the services unblocked though during the update for the scripts to work, so try the group policy editor first.

  2. I also import a new power plan, so the computer dose not go to sleep during the process. In the end I just exported the two powerplans, the one standard, and the one so that the computer did not go to sleep. You can just use the set active command, if you don't have other people using the computer, my way was just to rule out people changing the plan.

power_plan_set_high.bat

@echo on
powercfg -restoredefaultschemes 
powercfg -import "C:/TempCfg/Normal_Power_Plan.pow" 7aa3bc66-a968-4dd6-bbb9-24c28a3c7fa0
powercfg -import "C:/TempCfg/Automation_No_Sleep_Power_Plan.pow" a1ab2547-f518-4802-9912-2c447afe45cb
powercfg -SETACTIVE a1ab2547-f518-4802-9912-2c447afe45cb
pause

power_plan_set_normal.bat

@echo on
powercfg -restoredefaultschemes 
powercfg -import "C:/TempCfg/Normal_Power_Plan.pow" 7aa3bc66-a968-4dd6-bbb9-24c28a3c7fa0
powercfg -import "C:/TempCfg/Automation_No_Sleep_Power_Plan.pow" a1ab2547-f518-4802-9912-2c447afe45cb
powercfg -SETACTIVE 7aa3bc66-a968-4dd6-bbb9-24c28a3c7fa0
pause
  1. You may need to create some additional scripts to get your computer to login and log out and reboot also, for everything to run smoothly.

Further Reading:

Under A Tree

Posted 2019-06-10T19:24:39.470

Reputation: 849