In some situations, it's convenient to be able to check for Windows Updates without needing to launch the user interface, for instance if the system is currently in use and you need to work on it remotely.
The following Powershell will create a Microsoft.Update.Session API
instance and check for all updates that have not yet been installed:
$session = New-Object -ComObject 'Microsoft.Update.Session';
$searcher = $session.CreateUpdateSearcher();
<#
# Set $session.ServerSelection=1 to use the ManagedServer (eg. MWUS)
# Set $session.ServerSelection=2 to use Microsoft Update servers.
# See https://msdn.microsoft.com/en-us/library/aa387280(v=vs.85).aspx
#>
$searcher.ServerSelection=1;
$searcher; <# Show the configuration of the update searcher #>
Write 'Searching for new updates (this could take a while)...';
$search_result = $searcher.Search('isinstalled=0');
foreach ($update in $search_result.updates) {
Write $update.title
}
It will print out the title of all the updates, which will be the same list the user would see if they checked for updates manually, eg:
December, 2016 Security Only Quality Update for Windows Server 2012 R2 (KB3205400)
November, 2016 Security Only Quality Update for Windows Server 2012 R2 (KB3197873)
October, 2016 Security Only Quality Update for Windows Server 2012 R2 (KB3192392)
October, 2016 Security Only Update for .NET Framework 3.5 on Windows 8.1 and Windows Server 2012 R2 for x64 (KB3188732)
Microsoft Silverlight (KB4013867)
Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - March 2017 (KB890830)
Cumulative Security Update for Internet Explorer 11 for Windows Server 2012 R2 (KB4012204)
March, 2017 Security Monthly Quality Rollup for Windows Server 2012 R2 (KB4012216)
December, 2016 Security and Quality Rollup for .NET Framework 3.5, 4.5.2, 4.6, 4.6.1, 4.6.2 on Windows 8.1 and Windows Server 2012 R2 for x64 (KB3205404)
March, 2017 Preview of Monthly Quality Rollup for Windows Server 2012 R2 (KB4012219)
Update for Internet Explorer 11 for Windows Server 2012 R2 (KB4016446)
March, 2017 Security Only Quality Update for Windows Server 2012 R2 (KB4012213)
This can be done in a one-line mode through cmd.exe or Remote Tools' command prompt as well:
1 powershell -command "$session = new-object -comobject 'microsoft.update.session';$searcher = $session.createupdatesearcher();$searcher.ServerSelection=1; $searcher; write-host 'searching for new updates (this could take a while)'; $sres = $searcher.search('isinstalled=0'); foreach ($u in $sres.updates) {write-host $u.title};"
See https://msdn.microsoft.com/en-us/library/aa387099(v=vs.85).aspx for more information regarding the Windows Update API.