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:
- 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
- Create a new PowerShell script file, for example, 'Install-UpdatesOlderThan7Days.ps1', and open it in a text editor or PowerShell ISE.
- 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." } - Save the script file.
- 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
Post a Comment