Home > Enterprise >  script to remove unused drivers - pnputil
script to remove unused drivers - pnputil

Time:10-22

I have the following script which pulls the driver details and extracts to a text file driveroutput.txt, it then extracts the oem number from published name to a text file call oem.txt I then uses each line to delete the driver that oem#.inf that relates too.

$driveroutput = "C:\temp\driveroutput.txt"
$oemoutput = "C:\temp\oem.txt"
pnputil /enum-drivers > $driveroutput
Get-Content $driveroutput | Select-String -Pattern "Published Name" | %{$_.Line.Split(" ")} | Select-String -Pattern ".inf" | Where { $_ } | Set-Content $oemoutput
foreach($line in Get-Content $oemoutput) {
  pnputil.exe /delete-driver $line
}

example of the driveroutput.txt file

Published Name:     oem24.inf
Original Name:      ntprint.inf
Provider Name:      Microsoft
Class Name:         Printers
Class GUID:         {4d36e979-e325-11ce-bfc1-08002be10318}
Driver Version:     06/21/2006 10.0.17763.1217
Signer Name:        Microsoft Windows

Published Name:     oem11.inf
Original Name:      ntprint.inf
Provider Name:      Microsoft
Class Name:         Printers
Class GUID:         {4d36e979-e325-11ce-bfc1-08002be10318}
Driver Version:     06/21/2006 10.0.17763.771
Signer Name:        Microsoft Windows

Published Name:     oem13.inf
Original Name:      nvhda.inf
Provider Name:      NVIDIA Corporation
Class Name:         Sound, video and game controllers
Class GUID:         {4d36e96c-e325-11ce-bfc1-08002be10318}
Driver Version:     12/15/2017 1.3.36.6
Signer Name:        Microsoft Windows Hardware Compatibility Publisher

Published Name:     oem16.inf
Original Name:      nv_dispi.inf
Provider Name:      NVIDIA
Class Name:         Display adaptors
Class GUID:         {4d36e968-e325-11ce-bfc1-08002be10318}
Driver Version:     03/23/2018 23.21.13.9135
Signer Name:        Microsoft Windows Hardware Compatibility Publisher

example of the oem.txt file

oem24.inf
oem11.inf
oem13.inf
oem16.inf

Does anyone know how i could further filter by provider name that has the words "NVIDIA" in. The script i am looking to run would remove only the drivers that are nvidia related than all drivers

CodePudding user response:

You can get the drivers (the oem*.inf) without using a temporary text file.
In order to do that I'd suggest first joining all these lines with a NewLine so you end up with a single multiline string.

Then split this string into different sections on the double Newlines so you have a set of textblocks for each driver it returned we can parse out further.

Please read the inline comments on below code:

# First join the string array from pnputil with newlines and split on the empty lines
# that separate each driver. The result is a set of textblocks for per driver from which 
# we filter out the blocks where the wanted provider is mentioned.
$drivers = ((pnputil.exe /enum-drivers) -join [environment]::NewLine -split '(\r?\n){2,}' -ne '' | 
             Where-Object { $_ -match 'Provider Name:. \bNVIDIA\b' })

# test if we did find NVIDIA drivers
if ($drivers) {
    # Next replace the (first) colons with equal signs. Convert each block into a Hashtable using 
    # ConvertFrom-StringData and output the value of the property where the actual inf file is stored
    $drivers | ForEach-Object { 
        $driver = ($_ -replace '(?<!:.*):', '=' | ConvertFrom-StringData -ErrorAction SilentlyContinue).'Published Name'
        Write-Host "deleting driver using '$driver'"
        # Now you can delete the driver
        # for safety reasons I have commented this out:

        # pnputil.exe /delete-driver $driver
    }
}
else {
    Write-Host 'No NVIDIA drivers found'
}

Regex details for replacing the colon : into an equals sign =:

(?<!        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   :        Match the character “:” literally
   .        Match any single character
      *     Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)          
:           Match the character “:” literally
  • Related