📜 ⬆️ ⬇️

Script for removing old drivers

The script for Pavel Chubarov’s article automates the removal of outdated drivers from the C: \ windows \ system32 \ DriverStore \ FileRepository folder .

When installing drivers, old versions are saved in the system; this script removes all duplicates except the driver with the most recent date.

Maybe someone will come in handy.

The script gets the list of drivers in the system by executing the command:
')
dism /online /get-drivers 

Then there is a parsing of text output, the output will be normal objects with which you can work.

After duplicates are selected and sorted by date. The most recent driver is excluded from the list.

For each file in the list, the console utility pnputil is called with the option to remove the driver, if any driver is not removed, add the -f option.

in order to create a checkpoint just in case, execute the Checkpoint-Computer-Description “Driversdelete” command from the admin under the admin. To check whether the restore point was created, run the Get-ComputerRestorePoint cmdlet
script code here
Spoiler content
 <# 2016.09.01                   135        pnputil.exe -f -d #> #[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("cp866") #       #Checkpoint-Computer -Description "Driversdelete" #    $temp = dism /online /get-drivers $Lines = $temp | select -Skip 10 $Operation = "ItIsName" $Drivers = @() foreach ( $Line in $Lines ) { $temp1 = $Line $text = $($temp1.Split( ':' ))[1] switch ($Operation) { 'ItIsName' { $Name = $text $Operation = 'ItIsFileName' break } 'ItIsFileName' { $FileName = $text.Trim() $Operation = 'ItIsVhod' break } 'ItIsVhod' { $Vhod = $text.Trim() $Operation = 'ItIsClassName' break } 'ItIsClassName' { $ClassName = $text.Trim() $Operation = 'ItIsVendor' break } 'ItIsVendor' { $Vendor = $text.Trim() $Operation = 'ItIsDate' break } 'ItIsDate' { #     ,   $tmp = $text.split( '.' ) $text = "$($tmp[2]).$($tmp[1]).$($tmp[0].Trim())" $Date = $text $Operation = 'ItIsVersion' break } 'ItIsVersion' { $Version = $text.Trim() $Operation = 'ItIsNull' $params = [ordered]@{ 'FileName' = $FileName 'Vendor' = $Vendor 'Date' = $Date 'Name' = $Name 'ClassName' = $ClassName 'Version' = $Version 'Vhod' = $Vhod } $obj = New-Object -TypeName PSObject -Property $params $Drivers += $obj break } 'ItIsNull' { $Operation = 'ItIsName' break } } } Write-Host " " -ForegroundColor Yellow Write-Host "-------------------" -ForegroundColor Yellow $Drivers | sort Filename | ft Write-Host "  " -ForegroundColor Yellow Write-Host "-------------------" -ForegroundColor Yellow $last = '' $NotUnique = @() foreach ( $Dr in $($Drivers | sort Filename) ) { if ($Dr.FileName -eq $last ) { $NotUnique += $Dr } $last = $Dr.FileName } $NotUnique | sort FileName | ft Write-Host "  " -ForegroundColor Yellow Write-Host "-------------------" -ForegroundColor Yellow $list = $NotUnique | select -ExpandProperty FileName -Unique $ToDel = @() foreach ( $Dr in $list ) { Write-Host " " -ForegroundColor Yellow $sel = $Drivers | where { $_.FileName -eq $Dr } | sort date -Descending | select -Skip 1 $sel | ft $ToDel += $sel } Write-Host "  " -ForegroundColor Green Write-Host "-------------------" -ForegroundColor Green Write-Host " ,    " -ForegroundColor Green $ToDel | ft #   foreach ( $item in $ToDel ) { $Name = $($item.Name).Trim() Write-Host " " -ForegroundColor Green Write-Host " $Name" -ForegroundColor Green Write-Host "pnputil.exe -d $Name" -ForegroundColor Green Invoke-Expression -Command "pnputil.exe -d $Name" } 


Thanks for attention!

Source: https://habr.com/ru/post/319152/


All Articles