Install All Microsoft Updates Older Than 7 Days Using PowerShell and PSWindowsUpdate

To create a PowerShell script that leverages the PSWindowsUpdate module to install all Windows updates over 7 days old, follow these steps:

  1. Make sure the PSWindowsUpdate module is installed. If not, install it by running the following command in an elevated PowerShell session:
    Install-Module -Name PSWindowsUpdate
  2. Create a new PowerShell script file, for example, 'Install-UpdatesOlderThan7Days.ps1', and open it in a text editor or PowerShell ISE.
  3. Add the following code to the script file:
    # Import the PSWindowsUpdate module
    Import-Module PSWindowsUpdate
    
    # Set the date to 7 days ago
    $DateThreshold = (Get-Date).AddDays(-7)
    
    # Get the list of available updates that are older than 7 days
    $Updates = Get-WUList -NotCategory "Drivers" | Where-Object { $_.LastDeploymentChangeTime -lt $DateThreshold }
    
    # If updates are found, install them
    if ($Updates) {
        Write-Host "Updates found: $($Updates.Count)"
        Write-Host "Installing updates..."
    
        # Install the updates
        $InstallResult = Install-WindowsUpdate -Updates $Updates -AcceptAll -AutoReboot | Out-Null
    
        Write-Host "Updates installed successfully."
    } else {
        Write-Host "No updates older than 7 days found."
    }
    
  4. Save the script file.
  5. To run the script, open an elevated PowerShell session and navigate to the directory where the script file is saved. Execute the script by typing its name:
    .\Install-UpdatesOlderThan7Days.ps1

This script will check for updates older than 7 days and install them, excluding driver updates. If any updates are installed, the system will reboot automatically.

Comments

Popular posts from this blog

Unveiling the Power of PowerShell Regions: A Comprehensive Guide

PowerShell Script to Remotely Update Firmware on Brother Printers Microsoft

PowerShell Script to Reset Permissions on all Documents in a Document Library in SharePoint Online.