Windows Update using PowerShell on Windows 2012 R2 Server

I don’t see there is any build in PowerShell to perform Windows Update on Windows 2012 R2 Server, and after some research, I manage to find the great information on this topic from PowerShell for Windows Update

Please refer to the following PowerShell Script to download the PowerShell Module, extract and install it

#Download the PSWindowsUpdate.zip file 
$Source="https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc/file/41459/43/PSWindowsUpdate.zip"
$Destination = "c:\temp\PSWindowsUpdate.zip"
$UnzipTo = "c:\temp"
$PSModulePath="$env:windir\System32\WindowsPowerShell\v1.0\Modules"

Invoke-WebRequest $Source -OutFile $Destination -Verbose

#Function to unzip the PSWindowsUpdate.zip 
function unzip($file, $destination)
 {
 $shell = new-object -com shell.application
 $zip = $shell.NameSpace($file)

 foreach($item in $zip.items())
    {
        $shell.Namespace($destination).copyhere($item)
    }
 }

#Unzip the PSWindowsUpdate.zip to CC:\temp to "$env:windir\System32\WindowsPowerShell\v1.0\Modules"
unzip -file $Destination -destination $UnzipTo 

#Copy all the files from PSWindowsUpdate to 
Copy-Item -Recurse $UnzipTo\PSWindowsUpdate -Destination $PSModulePath

#Import the PSWinodwsUpdate Module 
Import-Module PSWindowsUpdate

#Get the Update server 
Get-WUServiceManager 
#Check, Download and Install Update 
#You will need to run this few time to ensure that there is no more update 
Get-WUInstall -Verbose

You will need to run “Get-WUInstall -Verbose” few time to ensure that there is no more update for Windows 2012 R2

DismCleanup-03

I will need to cleanup the downloaded Update in WinSxS folder by using the following commands ( “Desktop Experience” features is required to use the Disk Cleanup in GUI, and since I want to keep my Windows 2012 R2 Server as clean as possible with minimum space). I noticed that it take up 12.5GB of hard disk spaces with only the latest Windows updates installed

#Check how much disk space can be free up 
dism /online /cleanup-image /analyzecomponentstore 
#Clean up WinSxS Folder - it will take some time to complete 
dism /online /cleanup-image /startcomponentcleanup

With the above commands, it will helps to free up some disk spaces

DismCleanup-01

DismCleanup-02