0% found this document useful (0 votes)
8 views

AutoWindowsUpdate.ps1

The AutoWindowsUpdate.ps1 script automates the setup of Windows Update to check for updates every 30 minutes and restart the computer at 3:00 AM if necessary. It ensures the script runs with Administrator privileges, installs the PSWindowsUpdate module if not already present, and creates the required registry entries. Additionally, it registers two scheduled tasks: one for checking updates and another for restarting the computer if updates require it.

Uploaded by

Sajid Ali Khan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

AutoWindowsUpdate.ps1

The AutoWindowsUpdate.ps1 script automates the setup of Windows Update to check for updates every 30 minutes and restart the computer at 3:00 AM if necessary. It ensures the script runs with Administrator privileges, installs the PSWindowsUpdate module if not already present, and creates the required registry entries. Additionally, it registers two scheduled tasks: one for checking updates and another for restarting the computer if updates require it.

Uploaded by

Sajid Ali Khan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

### AutoWindowsUpdate.

ps1

# Ensure script runs as Administrator


If (-NOT ([Security.Principal.WindowsPrincipal]
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.Wi
ndowsBuiltInRole] "Administrator")) {
Write-Error "This script must be run as Administrator."
Exit
}

# Install PSWindowsUpdate module if not installed


If (-Not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
Install-PackageProvider -Name NuGet -Force
Install-Module -Name PSWindowsUpdate -Force
}

# Import the module


Import-Module PSWindowsUpdate

# Create the missing registry path if it does not exist


$RegistryPath = "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
If (-Not (Test-Path $RegistryPath)) {
Write-Host "Creating missing registry path: $RegistryPath"
New-Item -Path $RegistryPath -Force
}

# Configure Windows Update to auto-download and notify


Set-ItemProperty -Path $RegistryPath -Name "AUOptions" -Value 3 -Type DWord

# Create Scheduled Task for Update Check Every 30 Minutes


$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-
ExecutionPolicy Bypass -Command \"Import-Module PSWindowsUpdate; Get-WindowsUpdate
-MicrosoftUpdate -AcceptAll -Install\""
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date.AddMinutes(1) -
RepetitionInterval (New-TimeSpan -Minutes 30) -RepetitionDuration
([TimeSpan]::MaxValue)
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
-RunLevel Highest

# Ensure Action is created before registering the task


If ($Action) {
Write-Host "Registering Task: AutoWindowsUpdateCheck"
Register-ScheduledTask -TaskName "AutoWindowsUpdateCheck" -Action $Action -
Trigger $Trigger -Principal $Principal -Force
} Else {
Write-Error "Error: Action for AutoWindowsUpdateCheck not created."
}

# Create Scheduled Task for 3:00 AM Restart (if updates require reboot)
$RestartAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-
ExecutionPolicy Bypass -Command \"if ((Get-WmiObject
Win32_ComputerSystem).AutomaticManagedPagefile) { Restart-Computer -Force }\""
$RestartTrigger = New-ScheduledTaskTrigger -Daily -At "03:00AM"

# Ensure RestartAction is created before registering the task


If ($RestartAction) {
Write-Host "Registering Task: AutoWindowsUpdateRestart"
Register-ScheduledTask -TaskName "AutoWindowsUpdateRestart" -Action
$RestartAction -Trigger $RestartTrigger -Principal $Principal -Force
} Else {
Write-Error "Error: Action for AutoWindowsUpdateRestart not created."
}

Write-Output "\nAuto-update setup completed. Your PC will check for updates every
30 minutes and restart at 3:00 AM if required."
Read-Host -Prompt "Press Enter to exit"

You might also like