Script for deleting old drivers
a Scenario to state Pavel Chubarov automate delete obsolete drivers from the folder C:\windows\system32\DriverStore\FileRepository.
When installing drivers the old version saved in the system, this script deletes all duplicates except the driver which has the latest date
Can someone come in handy.
The script obtains the list of drivers in the system performing the command:
the
Then comes the parsing of the text output, the output is normal objects with which to work.
After you select the duplicates and sorted by date. The latest driver was removed from the list
For each file in the list is called with a command-line utility DLL parameter to remove the driver if full driver is not removed, add a key -f.
to create a checkpoint just in case, run the console PowerShell from the admin team Checkpoint-Computer-Description "Driversdelete". To check whether the created restore point, run Get-ComputerRestorePoint
Thank you for your attention!
Article based on information from habrahabr.ru
When installing drivers the old version saved in the system, this script deletes all duplicates except the driver which has the latest date
Can someone come in handy.
The script obtains the list of drivers in the system performing the command:
the
dism /online /get-driversThen comes the parsing of the text output, the output is normal objects with which to work.
After you select the duplicates and sorted by date. The latest driver was removed from the list
For each file in the list is called with a command-line utility DLL parameter to remove the driver if full driver is not removed, add a key -f.
to create a checkpoint just in case, run the console PowerShell from the admin team Checkpoint-Computer-Description "Driversdelete". To check whether the created restore point, run Get-ComputerRestorePoint
script code here
Content spoiler
the
the
<#
2016.09.01
lists the driver in the system
finds duplicates in the vault and deletes them
if you only need to look at the driver, comment out line 135
if the driver you need to uninstall forcibly use pnputil.exe -f-d
#>
#[Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("cp866")
#raskomentiruyte if you need to create a restore point
#Checkpoint-Computer-Description "Driversdelete"
# get the list of drivers
$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' { # translate the date in European standard, to sort
$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 "all drivers" -ForegroundColor Yellow
Write-Host "-------------------" -ForegroundColor Yellow
$Drivers | sort Filename | ft
Write-Host "a few drivers" -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 "legacy drivers" -ForegroundColor Yellow
Write-Host "-------------------" -ForegroundColor Yellow
$list = $NotUnique | select-ExpandProperty FileName is Unique
$ToDel = @()
foreach ( $Dr in $list ) {
Write-Host "found duplicate" -ForegroundColor Yellow
$sel = $Drivers | where { $_.FileName-eq $Dr } | sort date-Descending | select-Skip 1
$sel | ft
$ToDel += $sel
}
Write-Host "drivers to delete" -ForegroundColor Green
Write-Host "-------------------" -ForegroundColor Green
Write-Host "please be careful, any automatic actions are dangerous" -ForegroundColor Green
$ToDel | ft
# delete the driver
foreach ( $item in $ToDel ) {
$Name = $($item.Name).Trim()
Write-Host "" -ForegroundColor Green
Write-Host "removed $Name" -ForegroundColor Green
Write-Host "pnputil.exe -d $Name" -ForegroundColor Green
Invoke-Expression -Command "pnputil.exe -d $Name"
}
Thank you for your attention!
Комментарии
Отправить комментарий