Find-OSDriverByType.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
<#
.LINK
    https://www.osdeploy.com/psmodule/osdrivers/
.SYNOPSIS
    Finds specified file types, estimates size, option to remove Parent directory
.DESCRIPTION
    Finds specified file types, estimates size, option to remove Parent directory
.PARAMETER Path
    Directory to search for INF files. This should be your Driver Repository
.PARAMETER Type
    Select Drivers by Type
.PARAMETER RemoveSelected
    Removes the directory containing the file
.EXAMPLE
    Find-OSDriverByType -Type "Intel Wireless" -Path C:\OSDrivers\Intel
    Finds directories containing Intel Wireless Drivers
.EXAMPLE
    Find-OSDriverByType -Type "Intel Wireless" -Path C:\OSDrivers\Intel -RemoveSelected
    Finds directories containing Intel Wireless Drivers. Removes Parent directory of selected files
.NOTES
    NAME: Find-OSDriverByType.ps1
    AUTHOR: David Segura, david@segura.org
    BLOG: http://www.osdeploy.com
    CREATED: 02/18/2018
    VERSION: 1.1.0.2
#>


function Find-OSDriverByType
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$Path,
        [Parameter(Mandatory=$True)]
        [ValidateSet('AMD Video', 'Intel Bluetooth', 'Intel Ethernet', 'Intel Thunderbolt', 'Intel Video', 'Intel Wireless', 'Nvidia Video', 'Realtek Audio')]
        [string]$Type,
        [switch]$RemoveSelected
    )

    Write-Host "***** Calculating Size of $Path *****"
    $SizeStart = "{0:N2} GB" -f ((Get-ChildItem $Path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
    
    Write-Host $Type

    if ($Type -eq 'AMD Video') {$Files = "amdave32.dl*","amdkmpfd.inf"}
    if ($Type -eq 'Intel Bluetooth') {$Files = "ibt*.inf"}
    if ($Type -eq 'Intel Ethernet') {$Files = "e1*.inf"}
    if ($Type -eq 'Intel Thunderbolt') {$Files = "tbt*.inf"}
    if ($Type -eq 'Intel Video') {$Files = "igfx*.cpl"}
    if ($Type -eq 'Intel Wireless') {$Files = "netw*.inf"}
    if ($Type -eq 'Nvidia Video') {$Files = "nvapi.dl*"}
    if ($Type -eq 'Realtek Audio') {$Files = "hdxadc.inf","hd*.inf"}
    
    #Display the Files
    $a = Get-ChildItem -Path "$Path" -Recurse -Include ($Files) -File | Select-Object -Property Directory,Name,Length,FullName,CreationTime | Out-Gridview -Title 'Select INF Files to Estimate and Remove' -PassThru

    foreach ($i in $a) {

        #Set the Name of the Directory
        $DirName = $i.Directory.FullName

        if ( $DirName -eq $LastDirName ) {
            #We have already measured this directory

        }  Else {

            #Calculate the Size of the Directory
            $DirSize = (Get-ChildItem $DirName -Recurse | Measure-Object -Property length -sum).Sum

            $TotalSize = $TotalSize + $DirSize

            $DirSize = $DirSize /1MB

            Write-Host ("{0:N2} MB " -f $DirSize + $DirName)
            
                if ($RemoveSelected) {
                    Write-Host Deleting ($DirName + "\*")
                    Remove-Item -Path ($DirName + "\*") -Recurse
                }
            

            #Set the Directory Name
            $LastDirName = $DirName
        }
    }
    #Convert the Total Size to GB
    $TotalSize = $TotalSize /1GB

    if ($RemoveSelected) {
        Write-Host "***** Calculating Size of $Path *****"
        $SizeFinish = "{0:N2} GB" -f ((Get-ChildItem $Path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
        Write-Host "Old Repository Size: " $SizeStart
        Write-Host "New Repository Size: " $SizeFinish
    }  Else {
        Write-Host "Repository Size: " $SizeStart
        Write-Host ("{0:N2} GB" -f $TotalSize + " can be recovered")
    }
}