Why I want to do this:
When Windows Update ran without warning, hogged resources and rendered my computer nearly unusable for a long time,
I had used:
net stop wuauserv wmic service where name="TrustedInstaller" call stopservice taskkill /f /im TiWorker.exeAnd met serious consequence(update failed and kept being installed upon (re)boot and kept being rolled back), however it is important to keep my computer up to date for security.
What I want to achieve
Schedule a time to automatically scan, download and install updates and prevent unscheduled auto-update, only install updates while I am not using my computer, so that it will not slow down my PC.
What I had tried
cmd:
Reg Add HKLM\SYSTEM\CurrentControlSet\Services\TrustedInstaller /v Start /t REG_dword /d 3 /f
Reg Add HKLM\SYSTEM\CurrentControlSet\Services\UsoSvc /v Start /t REG_dword /d 3 /f
Reg Add HKLM\SYSTEM\CurrentControlSet\Services\WaaSMedicSvc /v Start /t REG_dword /d 3 /f
Reg Add HKLM\SYSTEM\CurrentControlSet\Services\wuauserv /v Start /t REG_dword /d 3 /fI can confirm these settings won't achieve my intended result.
gpedit.msc "Local Computer Policy"->"Computer Configuration"->"Administrative Templates"->"Windows Components"->"Windows Update"->"Configure Automatic Updates"
I had configured it to this:
(At 00:00 every Sunday on fourth week of every month)
And the effect:
If not all updates are restricted to the schedule, then it means nothing;
I know I can pause Windows Update but I doing it manually would be tiresome, though pausing Windows Update is useful. I am able to use taskschd to create scheduled tasks for script files, I want to programmatically pause Windows Update and schedule automatic update.
Update:
I have managed these with the help of @TOOGAM, using info from this link:
What I achieved:
$PauseStart=(Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$PauseEnd=(Get-Date).AddMonths(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -Name "PauseFeatureUpdatesEndTime" -Type "String" -Value $PauseEnd
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -Name "PauseQualityUpdatesEndTime" -Type "String" -Value $PauseEnd
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -Name "PauseUpdatesExpiryTime" -Type "String" -Value $PauseEnd
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -Name "PauseFeatureUpdatesStartTime" -Type "String" -Value $PauseStart
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -Name "PauseQualityUpdatesStartTime" -Type "String" -Value $PauseStart
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "AUOptions" -Type "DWord" -Value 2
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "NoAutoUpdate" -Type "DWord" -Value 1
Start-Service -Name "Wuauserv"However I can't let it automatically install updates, I had run this:
Get-Command -Module *Update*
CommandType Name Version Source
----------- ---- ------- ------
Function Get-WindowsUpdateLog 1.0.0.0 WindowsUpdate
Function Get-WUAVersion 1.0.0.2 WindowsUpdateProvider
Function Get-WUIsPendingReboot 1.0.0.2 WindowsUpdateProvider
Function Get-WULastInstallationDate 1.0.0.2 WindowsUpdateProvider
Function Get-WULastScanSuccessDate 1.0.0.2 WindowsUpdateProvider
Function Install-WUUpdates 1.0.0.2 WindowsUpdateProvider
Function Start-WUScan 1.0.0.2 WindowsUpdateProviderI had run this and encountered an error:
Start-WUScan
Invoke-CimMethod: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\WindowsUpdateProvider\MSFT_WUOperations.psm1:13
Line | 13 | … $scanres = Invoke-CimMethod -Namespace root/Microsoft/Windows/Window … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Provider load failure
Start-WUScan: Scan hit error: .ReturnValueAlso I am unable to make PowerShell manage scheduled tasks located in \Microsoft\Windows\WindowsUpdate in taskschd(although I can use schtasks in cmd and I know I can use cmd commands in pwsh but I want to do this in pure PowerShell spirit...)
I am able to use taskschd and I can create a scheduled task to run .ps1 files(just put "C:\Program Files\PowerShell\7\pwsh.exe" into program/script and use -file path\to\ps1 as argument)
How can I solve the problems that are in my way?
Update:
I had installed the Module: PSWindowsUpdate
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Install-Module -Name PSWindowsUpdateHad run this and here is the result:
Get-WindowsUpdate
ComputerName Status KB Size Title
------------ ------ -- ---- -----
UTILCOM-466… ------- KB4577194 607MB SQL Server 2019 RTM Cumulative Update (CU) 8 KB4577194
UTILCOM-466… -D----- KB4592438 103GB 2020-12 Cumulative Update for Windows 10 Version 20H2 for x64-based Systems (…Notice the size of KB4592438... It's 103GB...
Have a look at this:
What on earth is wrong?
I had put together this:
if ("PSWindowsUpdate" -notin (Get-Module -ListAvailable).name) {Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;Install-Module -Name PSWindowsUpdate}I know I can use:
Get-WindowsUpdate
Install-WindowsUpdateTheoretically it will work, but look at the size of KB4592438 shown by Get-WindowsUpdate...
Disclaimer:
I Know I can use this cmd one-liner to automatically scan, download and install Windows Update, but that isn't PowerShell and this is strictly against pure PowerShell spirit, I had explicitly stated that I intend to use "PowerShell Core 7" and "Task Scheduler" to achieve this and only using them, not cmd, which in my view isn't professional enough:
wuauclt /detectnow /updatenow 10 Reset to default