Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Automate Driver Updates via PowerShell Script in Windows

Keeping drivers up-to-date is crucial for maintaining system stability, performance, and security. This PowerShell script automates the process of searching, downloading, and installing driver updates on a Windows machine. The script interfaces with the Windows Update service to identify and install pending driver updates. It is particularly useful for IT administrators or power users who need to ensure that all drivers on a machine are current without manually checking for updates.


Examples:


1. Create the PowerShell Script:


You can save the script provided in the question as a .ps1 file, such as UpdateDrivers.ps1. Here's a breakdown of what the script does:




  • Add Windows Update Service: The script starts by creating a COM object to manage Windows Update services and adds a specific service required to fetch third-party drivers.


     $UpdateSvc = New-Object -ComObject Microsoft.Update.ServiceManager
    $UpdateSvc.AddService2("7971f918-a847-4430-9279-4a52d1efe18d", 7, "")



  • Search for Driver Updates: It then initiates a session and searcher to look specifically for drivers that are not yet installed.


     $Session = New-Object -ComObject Microsoft.Update.Session
    $Searcher = $Session.CreateUpdateSearcher()
    $Searcher.ServiceID = '7971f918-a847-4430-9279-4a52d1efe18d'
    $Searcher.SearchScope = 1 # MachineOnly
    $Searcher.ServerSelection = 3 # Third Party
    $Criteria = "IsInstalled=0 and Type='Driver'"
    $SearchResult = $Searcher.Search($Criteria)
    $Updates = $SearchResult.Updates



  • Display Available Driver Updates: If there are updates, the script will display details like the title, model, and version date of each available driver.


     $Updates | Select-Object Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer | Format-List



  • Download and Install the Updates: The script downloads and installs the drivers if they are not already installed.


     $UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl
    $Updates | ForEach-Object { $UpdatesToDownload.Add($_) | Out-Null }
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $UpdatesToDownload
    $Downloader.Download()

    $UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl
    $Updates | ForEach-Object {
    if ($_.IsDownloaded) {
    $UpdatesToInstall.Add($_) | Out-Null
    }
    }
    $Installer = $Session.CreateUpdateInstaller()
    $Installer.Updates = $UpdatesToInstall
    $InstallationResult = $Installer.Install()



  • Reboot if Necessary: After installation, if a reboot is required, the script will notify the user.


     if ($InstallationResult.RebootRequired) {
    Write-Host "Reinicialização necessária! Por favor, reinicie agora." -ForegroundColor Red
    } else {
    Write-Host "Concluído." -ForegroundColor Green
    }



  • Cleanup: Finally, the script removes the added update service to clean up after itself.


     $UpdateSvc.Services | Where-Object { $_.IsDefaultAUService -eq $false -and $_.ServiceID -eq "7971f918-a847-4430-9279-4a52d1efe18d" } | ForEach-Object {
    $UpdateSvc.RemoveService($_.ServiceID)
    }



2. Running the Script:



  • Open PowerShell as Administrator.

  • Run the script using the command:
     .\UpdateDrivers.ps1

  • Follow any on-screen prompts to complete the update process.


To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.