forked from bmrf/standalone_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_old_unused_drivers.ps1
124 lines (86 loc) · 3.25 KB
/
remove_old_unused_drivers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Removes old and unused versions of drivers from the Windows driver store
# Script source:
# https://www.itechtics.com/remove-old-drivers/
#Spacing/tabbing is messed up but I've tested the script and it works as of 2020-01-08
$dismOut = dism /online /get-drivers
$Lines = $dismOut | select -Skip 10
$Operation = "theName"
$Drivers = @()
foreach ( $Line in $Lines ) {
$tmp = $Line
$txt = $($tmp.Split( ':' ))[1]
switch ($Operation) {
'theName' { $Name = $txt
$Operation = 'theFileName'
break
}
'theFileName' { $FileName = $txt.Trim()
$Operation = 'theEntr'
break
}
'theEntr' { $Entr = $txt.Trim()
$Operation = 'theClassName'
break
}
'theClassName' { $ClassName = $txt.Trim()
$Operation = 'theVendor'
break
}
'theVendor' { $Vendor = $txt.Trim()
$Operation = 'theDate'
break
}
'theDate' { # change the date format for easy sorting
$tmp = $txt.split( '.' )
$txt = "$($tmp[2]).$($tmp[1]).$($tmp[0].Trim())"
$Date = $txt
$Operation = 'theVersion'
break
}
'theVersion' { $Version = $txt.Trim()
$Operation = 'theNull'
$params = [ordered]@{ 'FileName' = $FileName
'Vendor' = $Vendor
'Date' = $Date
'Name' = $Name
'ClassName' = $ClassName
'Version' = $Version
'Entr' = $Entr
}
$obj = New-Object -TypeName PSObject -Property $params
$Drivers += $obj
break
}
'theNull' { $Operation = 'theName'
break
}
}
}
Write-Host "All installed third-party drivers"
$Drivers | sort Filename | ft
Write-Host "Different versions"
$last = ''
$NotUnique = @()
foreach ( $Dr in $($Drivers | sort Filename) ) {
if ($Dr.FileName -eq $last ) { $NotUnique += $Dr }
$last = $Dr.FileName
}
$NotUnique | sort FileName | ft
Write-Host "Outdated drivers"
$list = $NotUnique | select -ExpandProperty FileName -Unique
$ToDel = @()
foreach ( $Dr in $list ) {
Write-Host "duplicate found" -ForegroundColor Yellow
$sel = $Drivers | where { $_.FileName -eq $Dr } | sort date -Descending | select -Skip 1
$sel | ft
$ToDel += $sel
}
Write-Host "Drivers to remove" -ForegroundColor Red
$ToDel | ft
# removing old drivers
foreach ( $item in $ToDel ) {
$Name = $($item.Name).Trim()
Write-Host "deleting $Name" -ForegroundColor Yellow
Write-Host "pnputil.exe -d $Name" -ForegroundColor Yellow
Invoke-Expression -Command "pnputil.exe -d $Name"
}