Get Windows Update Status Using PowerShell (Fast)

Get Windows Update Status Using PowerShell

Hello, PowerShell enthusiast today I will be sharing a script that will eventually help you to check various things on a server remotely after the windows server patching is performed. This script will fetch the results like server uptime, list of auto stopped services, list of KB articles installed on the server, etc. We will try to get the output in an HTML format so that we can send it to our clients in case it is required. 

Share on facebook
Share on twitter
Share on linkedin
Get Windows Update Status Using PowerShell

Script Requirements

Before we proceed further we need to understand some of the points which will help us to understand the script clearly.

  • To understand what exactly is New-Object PSObject go through the link to understand more on this.
  • Get-Hotfix which will be used to get the list of the installed patch.

How to get Windows Update Status Using PowerShell?

Step #1

 

If you are following me for a long you must be pretty sure I use the Foreach loop wildly and in this script as well I will be using the Foreach loop to get my required info.

$infoObject = @() and $results = @() will be used store the value in them. It will act as an array so that it will store the result from each server one by one.

				
					$comp= gc "provide the path of the server list"
$infoObject=@()
$results=@()
foreach($co in $comp)
{
				
			

Step #2

 

Not gonna lie but the below CSS code is taken from another project to format the HTML output at last. If you don’t want to use it you can skip this as it won’t harm the result.

				
					$co
$css = @"<style>h1, h5, th { text-align: center; font-family: Segoe UI; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }</style>"@
				
			

Step #3

 

$p test the connectivity of the servers from the list provided by you in Step #1. If the server is reachable then it will enter the if condition.

$os provides the operating system of the remote server like Windows Server 2008, Windows Server 2012 R2, etc.

$hotfix fetches the latest 3 patches from the remote servers.

 $v will align the latest 3 patches like KB1, KB2, KB3 so that it will be easy to go through at once.

				
					$infoObject = New-Object PSObject
$p=Test-Connection -ComputerName $co -BufferSize 16  -Count 1 -Quiet 
$p
if ($p -eq $true)
{
$os = (Get-WMIObject win32_operatingsystem -ComputerName $co ).caption
$os
$hotfix= Get-hotfix -ComputerName $co |select Hotfixid -Last 3
$v=($hotfix|select @{Name="fixes" ;expression={$hotfix.hotfixid -join ","}} -Last 1).fixes
$v
				
			

Step #4

 

$b will provide the last boot-up time of the remote server in a date and timely manner. 

$service will provide the Automatic service list from the remote server which is in the stopped state.

$r will join the list of services like service1, server2, service3, and so on.

$infoObject|Add-Member -MemberType NoteProperty -Name “Uptime” -value $b implies the result will be stored in the CSV format with entry Uptime and the value $b. It applies similarly to others as well.

$results+=$infoObject will store the $infoObject output to $results in each loop.

 

				
					$Boottime= Get-WmiObject win32_operatingsystem -computername $co
$b=($boottime| select @{LABEL="LastBootUpTime" ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}).Lastbootuptime
$b
$service=Get-WmiObject -ClassName Win32_Service -Filter "StartMode='Auto' AND State<>'Running'" -computername $co
$r=($service|select  @{Name="Service" ;expression={$service.name -join ","}} -Last 1).service
$r
$infoObject|Add-Member -MemberType NoteProperty -Name "Hostname"  -value $co
$infoObject|Add-Member -MemberType NoteProperty -Name "Reachable"  -value $p
$infoObject|Add-Member -MemberType NoteProperty -Name "Uptime"  -value $b
$infoObject|Add-Member -MemberType NoteProperty -Name "Operating System"  -value $os
$infoObject|Add-Member -MemberType NoteProperty -Name "Installed Patches" -Value $v
$infoObject|Add-Member -MemberType NoteProperty -Name "Auto Stopped services" -Value $r
$results+=$infoObject
}
				
			

Step #5

 

$b will provide the last boot-up time of the remote server in a date and timely manner. 

$service will provide the Automatic service list from the remote server which is in the stopped state.

$r will join the list of services like service1, server2, service3, and so on.

$infoObject|Add-Member -MemberType NoteProperty -Name “Uptime” -value $b implies the result will be stored in the CSV format with entry Uptime and the value $b. It applies similarly to others as well.

$results+=$infoObject will store the $infoObject output to $results in each loop.

				
					else
{

$infoObject|Add-Member -MemberType NoteProperty -Name "Hostname"  -value $co
$infoObject|Add-Member -MemberType NoteProperty -Name "Reachable"  -value $p
#$infoObject|Add-Member -MemberType NoteProperty -Name "Uptime"  -value $b
#$infoObject|Add-Member -MemberType NoteProperty -Name "Operating System"  -value $os
#$infoObject|Add-Member -MemberType NoteProperty -Name "Installed Patches" -Value $v
#$infoObject|Add-Member -MemberType NoteProperty -Name "Auto Stopped services" -Value $r
$results+=$infoObject

}
}

$results|Export-csv "C:\Users\admin\Desktop\result.csv" -NoTypeInformation 
Import-CSV "C:\Users\admin\Desktop\result.csv" | ConvertTo-Html -Head $css  | Out-File "C:\Users\admin\Desktop\postreportpatch.html" 
				
			

Result of the PowerShell script

Below is the result of the script and as you can see it will create an HTML file ad thank god we have added the CSS code so that it looks good. Attaching the snap below.

Conclusion

I hope you have liked the post Get Windows Update Status Using PowerShell (Fast) and will implement this whenever it is required. Things you should always remember is to try with one server so that it will be easy for you to make changes. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newsletter. 

Advertisement. Scroll to continue reading.
close
blank

DON’T MISS A POST

Keep up to date with PowershellGuru

SSShhhhhhh.........We don’t spam!

Powershell Blogs
Get Windows Update Status Using PowerShell
Share via
Copy link