Saturday, May 17, 2025

Manual Windows Update From PowerShell

I am so used to linux that I was looking for a simple solution in windows to do $ sudo apt update -y && sudo apt upgrade -y . From Powershell or from commandline. 

Normally first we must install the windows update module. Then load it into powershell. Then Launch it to search, then launch it to update . A little more complicated, a little longer and way slower than apt update and apt upgrade. But it works. 

Step-by-step:

a. Open PowerShell as Administrator, then install the module:

Install-Module -Name PSWindowsUpdate -Force

b. Import the module (if needed):

Import-Module PSWindowsUpdate

c. Check for updates:

Get-WindowsUpdate

d. Install updates:

Install-WindowsUpdate -AcceptAll -AutoReboot

You can also skip the reboot:

Install-WindowsUpdate -AcceptAll -IgnoreReboot

In some cases, when windows update in GUI doesn't want to install or it causes problems, stops, misbehaves, in powershell we can still run them. 

 

HERE IS A POWERSHELL SCRIPT FOR A FULL ON UPDATE PROCEDURE

Please allow script execution before !

 
 # Ensure script can run by temporarily setting execution policy
Try {
    $currentPolicy = Get-ExecutionPolicy -Scope Process
    If ($currentPolicy -ne 'Bypass') {
        Write-Host "[INFO] Setting execution policy to Bypass for this session..."
        Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
    }
} Catch {
    Write-Host "[ERROR] Failed to set execution policy: $_"
    Exit 1
}

Function Write-Log {
    param([string]$Message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Write-Host "[$timestamp] $Message"
}

# Check for admin privileges
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Log "ERROR: Please run this script as Administrator."
    Exit 1
}

Write-Log "Starting Windows update check..."

# Ensure NuGet and PSWindowsUpdate
Try {
    If (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
        Write-Log "Installing NuGet..."
        Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
    } else {
        Write-Log "NuGet already present."
    }

    If (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) {
        Write-Log "Installing PSWindowsUpdate module..."
        Install-Module -Name PSWindowsUpdate -Force -Scope CurrentUser
    } else {
        Write-Log "PSWindowsUpdate module already present."
    }

    Import-Module PSWindowsUpdate -Force
    Write-Log "Module imported. Checking for updates..."

    $updates = Get-WindowsUpdate

    If ($updates.Count -eq 0) {
        Write-Log "No updates available. Your system is up to date."
        Exit 0
    }

    Write-Log "The following updates are available:"
    $updates | ForEach-Object { Write-Log " → $($_.Title)" }

    # Prompt the user
    $confirmation = Read-Host "`nDo you want to install these updates now? [Y/n]"

    If ($confirmation -eq "Y" -or $confirmation -eq "y" -or $confirmation -eq "") {
        Write-Log "Installing updates..."
        Install-WindowsUpdate -AcceptAll -AutoReboot -Verbose
        Write-Log "Updates installed. Reboot may occur automatically."
    } else {
        Write-Log "User cancelled update installation."
        Exit 0
    }
}
Catch {
    Write-Log "ERROR: $_"
    Exit 1
}

No comments:

Post a Comment

Sharepoint sync and lock issues - solutions

 Sharepoint synced down to your PC by onedrive can have a ton of errors. It is caused by the simple dis-functionality of this badly thought ...