Install app updates and Windows Updates
copy the entire text below and paste into powershell script.
Installs all app updates available via winget
Installs all available Windows updates from Microsoft
Automate reboot if needed.
copy the entire text below and paste into powershell script.
Installs all app updates available via winget
Installs all available Windows updates from Microsoft
Automate reboot if needed.
# Ensure script is run as Administrator
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Error "Run this script as Administrator."
exit
}
Write-Output "**===== vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv =====**"
Write-Output "**===== =====**"
Write-Output "**--> Windows will auto-reboot for update installation <--**"
Write-Output "**===== Run this script again after reboot =====**"
Write-Output "**===== =====**"
Write-Output "**===== ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ =====**"
sleep 5
# Timestamp for logs: yyyy.MM.dd.HH.mm
$timestamp = (Get-Date).ToString("yyyy.MM.dd.HH.mm")
# Paths
$basePath = "C:\temp\windowsupdatelogs"
$logPath = "$basePath\archived-logs"
Write-Output "`n=== Preparing Log Directory ==="
# Create log directory if needed
if (-not (Test-Path $logPath)) {
New-Item -Path $logPath -ItemType Directory -Force | Out-Null
Write-Output "Created log directory: $logPath"
} else {
Write-Output "Log directory already exists: $logPath"
}
# Move previous update logs to archive
Write-Output "Archiving previous update logs..."
Get-ChildItem -Path $basePath -Filter "*WindowsUpdateInstall*.log" | Move-Item -Destination $logPath -Force
Get-ChildItem -Path $basePath -Filter "*WUList*.txt" | Move-Item -Destination $logPath -Force
Get-ChildItem -Path $basePath -Filter "*PSWindowsUpdateInstall*.log" | Move-Item -Destination $logPath -Force
# Upgrade all winget packages
Write-Output "`n=== Running Winget Upgrade ==="
winget upgrade --all --accept-package-agreements --accept-source-agreements
# Install PSWindowsUpdate module
Write-Output "`n=== Installing PSWindowsUpdate Module ==="
Install-Module -Name PSWindowsUpdate -Force -Verbose | Out-File "$basePath\PSWindowsUpdateInstall-$timestamp.log"
Import-Module PSWindowsUpdate -Verbose
# Reset Windows Update components
Write-Output "Resetting Windows Update components..."
Reset-WUComponents
Write-Output "`n=== Adding Windows Update Sources ==="
# Define known update sources with labels
$updateSources = @(
@{ ID = "7971f918-a847-4430-9279-4a52d1efe18d"; Name = "Microsoft Update" },
@{ ID = "8b24b027-1dee-babb-9a95-3517dfb9c552"; Name = "DCat Flighting Prod" },
@{ ID = "855e8a7c-ecb4-4ca3-b045-1dfa50104289"; Name = "Windows Store (DCat Prod)" },
@{ ID = "9482f4b4-e343-43b6-b170-9a65bc822c77"; Name = "Windows Update" }
)
foreach ($source in $updateSources) {
$id = $source.ID
$name = $source.Name
try {
Add-WUServiceManager -ServiceID $id -AddServiceFlag 7 -Confirm:$False -ErrorAction SilentlyContinue
$status = Get-WUServiceManager | Where-Object { $_.ServiceID -eq $id }
if ($status) {
$enabled = $status.IsDefaultAUService
$registered = $status.IsRegisteredWithAU
Write-Output "$id`t$enabled`t$registered`t$name"
} else {
Write-Warning "Could not retrieve status for: $name ($id)"
}
} catch {
Write-Warning "Failed to add update source: $name ($id)"
}
}
# Add update sources
Write-Output "`n=== Adding Update Sources ==="
foreach ($id in $serviceIDs) {
Write-Output "`n=== Adding Update Source $id ==="
Add-WUServiceManager -ServiceID $id -AddServiceFlag 7 -Confirm:$False
}
# Get update lists
Write-Output "`n=== Checking for Available Updates ==="
foreach ($id in $serviceIDs) {
Get-WUList -ServiceID $id -Verbose | Out-File "$basePath\WUList-$timestamp.txt"
}
Write-Output "`n=== Installing Updates and Checking Reboot Requirement ==="
# Run update and capture result
$updateResult = Install-WindowsUpdate -ServiceID $id -AcceptAll -Verbose -Confirm:$False -IgnoreReboot | Tee-Object -FilePath "$basePath\WindowsUpdateInstall-$timestamp.log"
# Check if reboot is required
if ($updateResult -match "Reboot Required: True") {
Write-Output "`n ===Updates require a reboot. System will restart in 90 seconds.==="
Start-Sleep -Seconds 90
Restart-Computer -Force
} else {
Write-Output "`n No reboot required after updates."
}
Write-Output "`n [|---> Windows Setup and Update process complete. Computer is ready for use. <---|]"