BlueCarbon

WinUp.ps1

Jul 27th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 10.75 KB | Software | 0 0
  1. Function Write-Banner
  2. {
  3.     [CmdletBinding()]
  4.     param (
  5.         [Parameter(ValueFromPipeline=$true)]
  6.         [String]$Message
  7.     )
  8.     '#' * ($Message).Length
  9.      Write-Host $Message
  10.     '#' * ($Message).Length
  11.      Write-Host ''
  12. }
  13.  
  14. $updateSession = new-object -com 'Microsoft.Update.Session'
  15. $UpdateDownloader = $UpdateSession.CreateUpdateDownloader()
  16. $UpdateInstaller = $UpdateSession.CreateUpdateInstaller()
  17. $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
  18.  
  19. # Find updates AND INCLUDE Drivers
  20. $criteria = "IsInstalled=0 AND IsHidden=0 AND DeploymentAction=*"
  21.  
  22. $search = $UpdateSearcher.Search(($criteria))
  23.  
  24. $h1 = 'Category'
  25. $h2 = 'Title'
  26. $h3 = 'Description'
  27. $Catalog=@()
  28. $UpdatesExist = $false
  29. $AvailableUpdates = $Search.Updates
  30.  
  31. If ($AvailableUpdates.count -gt 0)
  32. {
  33.     $UpdatesExist = $true
  34.  
  35.     Foreach ($Update in $AvailableUpdates)
  36.     {
  37.         $Table = '' | Select-Object $h1,$h2,$h3
  38.         $Index = $Update.Categories.Item.count - 1
  39.         $Item = $Update.Categories.Item($Index)
  40.         $Category = $Item.Name
  41.         $Title = $Update.Title
  42.         $Table.$h1 = $Category
  43.         $Table.$h2 = $Update.Title
  44.         $Table.$h3 = $Update.Description
  45.         $Catalog += $Table
  46.     }
  47.    
  48.     $Group = $Catalog | Group-Object -Property 'Category'
  49.    
  50.     Foreach ($Member in $Group)
  51.     {
  52.         $Title = $Member.Name
  53.         Write-Host "[${Title}]" -ForegroundColor Yellow
  54.         $Member.Group | Foreach-Object {
  55.             Write-Host ' - ' $_.Title -ForegroundColor Cyan
  56.             Write-Host '    ' $_.Description
  57.             Write-Host ''
  58.         }
  59.         Write-Host ''
  60.     }
  61.  
  62.     $Time = (Get-Date).ToString('HH:mm:ss')
  63.     Write-Host "[${Time}] Downloading Updates ..."
  64.     $UpdateDownloader.Updates = $AvailableUpdates
  65.     # Commented for TESTING. Uncomment to download the updates.
  66.     #$UpdateDownloader.Download()
  67.    
  68.     $Time = (Get-Date).ToString('HH:mm:ss')
  69.     Write-Host "[${Time}] Installing Updates ..."
  70.     $UpdateInstaller.Updates = $UpdateDownloader.Updates
  71.     # Commented for TESTING. Uncomment to install the updates.
  72.     #$UpdateInstaller.Install()
  73. }
  74.  
  75. if ( -not ($UpdatesExist) )
  76. {
  77.     Write-Banner 'No new updates available'
  78. }
  79.  
  80. if ($UpdateInstaller.RebootRequiredBeforeInstallation)
  81. {
  82.     Write-Banner 'Reboot Required Before Installation'
  83. }
  84.  
  85. $SystemInfo = New-Object -com 'Microsoft.Update.SystemInfo'
  86. if ($SystemInfo.RebootRequired)
  87. {
  88.     Write-Banner 'Reboot Required'
  89.     # Optionally include a command to restart with a delay of X number of seconds
  90.     # &cmd /C "shutdown -r -f -t X"
  91. }
  92.  
  93.  
  94.  
  95. <#
  96.  
  97.     AVAILABLE OPTIONS FOR SEARCH CRITERIA
  98.     Note: The default search criteria is, "IsInstalled=0 and IsHidden=0"
  99.  
  100.     Criterion: Type
  101.     Type: String
  102.     Acceptable operators: =,!=  (equals, not equals)
  103.     Finds updates of a specific type, such as "'Driver'" and "'Software'".
  104.     For example, you may exclude drivers with "Type='Software' AND Type!='Driver'"
  105.     You may also search for drivers only by specifying "Type='Driver'" and not including 'Software'
  106.  
  107.     Criterion: DeploymentAction
  108.     Type: String
  109.     Acceptable Operators: =
  110.     Finds updates that are deployed for a specific action, such as an installation or uninstallation that the administrator of a server specifies.
  111.     "DeploymentAction='Installation'" finds updates that are deployed for installation on a destination computer.
  112.     "DeploymentAction='Uninstallation'" finds updates that are deployed for uninstallation on a destination computer.
  113.     "DeploymentAction=*" finds updates that are deployed for installation and uninstallation on a destination computer.
  114.     If this criterion is not explicitly specified, each group of criteria that is joined to an AND operator implies "DeploymentAction='Installation'".
  115.     "DeploymentAction='Uninstallation'" depends on the other query criteria.
  116.  
  117.     Criterion: IsAssigned
  118.     Type: int(bool)
  119.     Acceptable Operators: =
  120.     Finds updates that are intended for deployment by Automatic Updates.
  121.     "IsAssigned=1" finds updates that are intended for deployment by Automatic Updates, which depends on the other query criteria. At most, one assigned Windows-based driver update is returned for each local device on a destination computer.
  122.     "IsAssigned=0" finds updates that are not intended to be deployed by Automatic Updates.
  123.  
  124.     Criterion: BrowseOnly
  125.     Type: int(bool)
  126.     Acceptable Operators: =
  127.     "BrowseOnly=1" finds updates that are considered optional.
  128.     "BrowseOnly=0" finds updates that are not considered optional.
  129.  
  130.     Criterion: AutoSelectOnWebSites
  131.     Type: int(bool)
  132.     Acceptable Operators: =
  133.     Finds updates where the AutoSelectOnWebSites property has the specified value.
  134.     "AutoSelectOnWebSites=1" finds updates that are flagged to be automatically selected by Windows Update.
  135.     "AutoSelectOnWebSites=0" finds updates that are not flagged for Automatic Updates.
  136.  
  137.     Criterion: UpdateID
  138.     Type: string(UUID)
  139.     Acceptable Operators: =
  140.     Acceptable operators: =,!=  (equals, not equals)
  141.     Finds updates for which the value of the UpdateIdentity.UpdateID property matches the specified value. Can be used with the != operator to find all the updates that do not have an UpdateIdentity.UpdateID of the specified value.
  142.     For example, "UpdateID='12345678-9abc-def0-1234-56789abcdef0'" finds updates for UpdateIdentity.UpdateID that equal 12345678-9abc-def0-1234-56789abcdef0.
  143.     For example, "UpdateID!='12345678-9abc-def0-1234-56789abcdef0'" finds updates for UpdateIdentity.UpdateID that are not equal to 12345678-9abc-def0-1234-56789abcdef0.
  144.     Note  A RevisionNumber clause can be combined with an UpdateID clause that contains an = (equal) operator. However, the RevisionNumber clause cannot be combined with an UpdateID clause that contains the != (not-equal) operator.
  145.     For example, "UpdateID='12345678-9abc-def0-1234-56789abcdef0' and RevisionNumber=100" can be used to find the update for UpdateIdentity.UpdateID that equals 12345678-9abc-def0-1234-56789abcdef0 and whose UpdateIdentity.RevisionNumber equals 100.
  146.  
  147.     Criterion: RevisionNumber
  148.     Type: int
  149.     Acceptable Operators: =
  150.     Finds updates for which the value of the UpdateIdentity.RevisionNumber property matches the specified value.
  151.     For example, "RevisionNumber=2" finds updates where UpdateIdentity.RevisionNumber equals 2.
  152.     This criterion must be combined with the UpdateID property.
  153.  
  154.     Criterion: CategoryIDs
  155.     Type: string(UUID)
  156.     Acceptable Operators: CONTAINS
  157.     Finds updates that belong to a specified category.
  158.         Application         5C9376AB-8CE6-464A-B136-22113DD69801
  159.         Connectors          434DE588-ED14-48F5-8EED-A15E09A991F6
  160.         CriticalUpdates     E6CF1350-C01B-414D-A61F-263D14D133B4
  161.         DefinitionUpdates   E0789628-CE08-4437-BE74-2495B842F43B
  162.         DeveloperKits       E140075D-8433-45C3-AD87-E72345B36078
  163.         Drivers             EBFC1FC5-71A4-4F7B-9ACA-3B9A503104A0
  164.         FeaturePacks        B54E7D24-7ADD-428F-8B75-90A396FA584F
  165.         Guidance            9511D615-35B2-47BB-927F-F73D8E9260BB
  166.         HotFix              5EAEF3E6-ABB0-4192-9B26-0FD955381FA9
  167.         SecurityUpdates     0FA1201D-4330-4FA8-8AE9-B877473B6441
  168.         ServicePacks        68C5B0A3-D1A6-4553-AE49-01D3A7827828
  169.         ThirdParty          871A0782-BE12-A5C4-C57F-1BD6D9F7144E
  170.         Tools               B4832BD8-E735-4761-8DAF-37F882276DAB
  171.         UpdateRollups       28BC880E-0592-4CBF-8F95-C79B17911D5F
  172.         Updates             CD5FFD1E-E932-4E3A-BF74-18BF0B1BBD83
  173.         Upgrades            3689BDC8-B205-4AF4-8D4A-A63924C5E9D5
  174.     For example, "CategoryIDs contains 'B54E7D24-7ADD-428F-8B75-90A396FA584F'" finds Feature Packs
  175.  
  176.     Criterion: IsInstalled
  177.     Type: int(bool)
  178.     Acceptable Operators: =
  179.     Finds updates that are installed on the destination computer.
  180.     "IsInstalled=1" finds updates that are installed on the destination computer.
  181.     "IsInstalled=0" finds updates that are not installed on the destination computer.
  182.  
  183.     Criterion: IsHidden
  184.     Type: int(bool)
  185.     Acceptable Operators: =
  186.     Finds updates that are marked as hidden on the destination computer.
  187.     "IsHidden=1" finds updates that are marked as hidden on a destination computer. When you use this clause, you can set the UpdateSearcher.IncludePotentiallySupersededUpdates property to VARIANT_TRUE so that a search returns the hidden updates. The hidden updates might be superseded by other updates in the same results.
  188.     "IsHidden=0" finds updates that are not marked as hidden. If the UpdateSearcher.IncludePotentiallySupersededUpdates property is set to VARIANT_FALSE, it is better to include that clause in the search filter string so that the updates that are superseded by hidden updates are included in the search results. VARIANT_FALSE is the default value.
  189.  
  190.     Criterion: IsPresent
  191.     Type: int(bool)
  192.     Acceptable Operators: =
  193.     When set to 1, finds updates that are present on a computer.
  194.     "IsPresent=1" finds updates that are present on a destination computer. If the update is valid for one or more products, the update is considered present if it is installed for one or more of the products.
  195.     "IsPresent=0" finds updates that are not installed for any product on a destination computer.
  196.  
  197.     Criterion: RebootRequired
  198.     Type: int(bool)
  199.     Acceptable Operators: =
  200.     Finds updates that require a computer to be restarted to complete an installation or uninstallation.
  201.     "RebootRequired=1" finds updates that require a computer to be restarted to complete an installation or uninstallation.
  202.     "RebootRequired=0" finds updates that do not require a computer to be restarted to complete an installation or uninstallation.
  203.  
  204.  
  205.     Display all applicable updates, even those superseded by newer updates
  206.     $UpdateSearcher.IncludePotentiallySupersededUpdates = $true
  207.  
  208.     Display ALL hidden updates you must include superseded and specify the IsHidden Criteria
  209.     $UpdateSearcher.IncludePotentiallySupersededUpdates = $true
  210.     ( IsHidden = 1 )
  211.  
  212.  
  213.     Example criteria: Find updates that are NOT installed and NOT marked as hidden
  214.     $criteria = "IsInstalled=0 and IsHidden=0"
  215.  
  216.     Find updates and EXCLUDE Drivers
  217.     $criteria = "IsInstalled=0 AND IsHidden=0 AND Type='Software' AND DeploymentAction=*"
  218.  
  219.     Find updates and limit search to FeaturePacks only
  220.     $criteria = "IsInstalled=0 AND IsHidden=0 AND DeploymentAction=* AND CategoryIDs contains 'B54E7D24-7ADD-428F-8B75-90A396FA584F'"
  221.  
  222.     You can also include 'or' as an operator to create multiple query types
  223.     $criteria = "IsInstalled=0 and DeploymentAction='Installation' or IsPresent=1 and DeploymentAction='Uninstallation' or IsInstalled=1 and DeploymentAction='Installation' and RebootRequired=1 or IsInstalled=0 and DeploymentAction='Uninstallation' and RebootRequired=1"
  224.  
  225. #>
Advertisement
Add Comment
Please, Sign In to add comment