Find-OSDriverByType.ps1

<#
.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/17/2018
    VERSION: 1.1.0.0
#>


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")
    }
}