Streamlining Windows Updates: Automate Like a Pro with PowerShell

Streamlining Windows Updates: Automate Like a Pro with PowerShell

Introduction

In today’s rapidly evolving digital landscape, keeping your Windows systems up-to-date is essential for security, performance, and functionality. However, manually managing these updates can be time-consuming and prone to errors. Thankfully, PowerShell provides a powerful solution to automate this process, ensuring that your systems are always running smoothly and securely. Let’s explore how you can leverage the power of PowerShell to streamline and automate Windows updates effortlessly.

Automating Windows Updates with PowerShell

Ensuring your Windows systems are up-to-date is crucial for security and performance. Manual updates, however, can be tedious and prone to oversight. This PowerShell script automates the update process, making it efficient and reliable. Before running the script, ensure your system meets the prerequisites and that you have administrative privileges. Let’s dive into how you can automate Windows updates with ease.

# script begins

# Logger

function Log-Message {

[CmdletBinding()]

param(

[Parameter(Mandatory = $true)]

[ValidateSet(‘INFO’, ‘DEBUG’, ‘ERROR’)]

[string]$LogLevel,

[Parameter(Mandatory = $true)]

[string]$Message

)

$Timestamp = Get-Date -Format “yyyy-MM-dd HH:mm:ss”

Write-Output “$Timestamp [$LogLevel] $Message”

}

Log-Message -LogLevel “INFO” -Message “Installing PSWindowsUpdate module”

Install-Module PSWindowsUpdate

Log-Message -LogLevel “INFO” -Message “Installed PSWindowsUpdate module”

Log-Message -LogLevel “INFO” -Message “Checking Windows Update Service status”

Get-Service wuauserv

$wuauservStatus = (Get-Service wuauserv).Status

if($wuauservStatus -eq “Stopped”)

{

Write-Host “Starting the service…”

Start-Service wuauserv

Get-Service wuauserv

}

#Get the list of Updates available

Get-WindowsUpdate

# Create Windows Update Session

$windowsUpdateSession = New-Object -ComObject Microsoft.Update.Session

Log-Message -LogLevel “INFO” -Message “Windows Update Session created”

# Create Windows Update Searcher

$windowsUpdateSearcher = $windowsUpdateSession.CreateUpdateSearcher()

Log-Message -LogLevel “INFO” -Message “Windows Update Searcher created”

# Search for available updates

$updates = $windowsUpdateSearcher.Search(“IsInstalled=0”)

# Check if updates are available

if ($updates.Updates.Count -eq 0) {

Write-Host “No updates available”

Log-Message -LogLevel “INFO” -Message “No updates available”

} else {

Write-Host “Found $($updates.Updates.Count) update(s)”

Log-Message -LogLevel “INFO” -Message “Found $($updates.Updates.Count) update(s)”

# Create Windows Update Downloader

$downloader = $windowsUpdateSession.CreateUpdateDownloader()

$downloader.Updates = $updates.Updates

# Download updates

Write-Host “Downloading updates…”

Log-Message -LogLevel “INFO” -Message “Downloading updates…”

# $downloadResult = $downloader.Download()

if ($downloadResult.Hresult -eq 0) {

Write-Host “Updates downloaded successfully”

Log-Message -LogLevel “INFO” -Message “Updates downloaded successfully”

} else {

Write-Host “Failed to download updates. Error: $($downloadResult.Hresult)”

Log-Message -LogLevel “ERROR” -Message “Failed to download updates. Error: $($downloadResult.Hresult)”

exit

}

# Create Windows Update Installer

$installer = $windowsUpdateSession.CreateUpdateInstaller()

$installer.Updates = $updates.Updates

# Install updates

Write-Host “Installing updates…”

Log-Message -LogLevel “INFO” -Message “Installing updates…”

# $installResult = $installer.Install()

if ($installResult.ResultCode -eq 2) {

Write-Host “Installation successful. Reboot required.”

Log-Message -LogLevel “INFO” -Message “Installation successful. Reboot required.”

Write-Host “Rebooting…”

Restart-Computer -Force

} elseif ($installResult.ResultCode -eq 3) {

Write-Host “Installation successful. No reboot required.”

Log-Message -LogLevel “INFO” -Message “Installation successful. No reboot required.”

} else {

Write-Host “Failed to install updates. Result code: $($installResult.ResultCode)”

Log-Message -LogLevel “ERROR” -Message “Failed to install updates. Result code: $($installResult.ResultCode)”

exit

}

}

Log-Message -LogLevel “INFO” -Message “Windows updated successfully”

# script ends

Thank you for taking the time to read my blog. Your feedback is immensely valuable to me. Please feel free to share your thoughts and suggestions.