删除未使用的驱动程序的脚本 - pnputil

script to remove unused drivers - pnputil

我有以下脚本,它提取驱动程序详细信息并提取到文本文件 driveroutput.txt,然后从发布的名称中提取 oem 编号到文本文件调用 oem.txt 然后我使用每一行来删除与 oem#.inf 相关的驱动程序。

$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
}

driveroutput.txt 文件的示例

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

oem.txt 文件的示例

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

有谁知道我如何进一步过滤包含“NVIDIA”字样的提供商名称。 我正在寻找的脚本 运行 将仅删除与 nvidia 相关的驱动程序而不是所有驱动程序

您可以在不使用临时文本文件的情况下获取驱动程序(oem*.inf)。
为了做到这一点,我建议首先使用 NewLine 连接所有这些行,这样您就可以得到一个多行字符串。

然后将此字符串拆分为双换行符上的不同部分,这样您就可以为它返回的每个驱动程序提供一组文本块,我们可以进一步解析。

请阅读以下代码的内联注释:

# 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'
}

将冒号 : 替换为等号 = 的正则表达式详细信息:

(?<!        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