Find-OSDriverINFs.ps1

<#
.LINK
    https://www.osdeploy.com/psmodule/osdrivers/
.SYNOPSIS
    Finds specified files, estimates size, option to remove Parent directory
.DESCRIPTION
    Finds specified files, estimates size, option to remove Parent directory
.PARAMETER Path
    Directory to search for INF files
.PARAMETER Files
    Files to Include. Default is *.inf
.PARAMETER RemoveSelected
    Removes the directory containing the file
.EXAMPLE
    Find-OSDriverINFs -Path C:\OSDrivers\Intel
    Finds all INF files in C:\OSDrivers\Intel
.EXAMPLE
    Find-OSDriverINFs -Path C:\OSDrivers\Intel -RemoveSelected
    Finds all INF files in C:\OSDrivers\Intel. Removes Parent directory of selected files
.NOTES
    NAME: Find-OSDriverINFs.ps1
    AUTHOR: David Segura, david@segura.org
    BLOG: http://www.osdeploy.com
    CREATED: 02/17/2018
    VERSION: 1.1.0.1
#>


function Find-OSDriverINFs
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$Path,
        [string]$Files = '*.inf',
        [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)
    
    #Display the Files
    $a = Get-ChildItem -Path "$Path" -Recurse $Files -File | Select-Object -Property Directory,Name,Length,FullName,CreationTime | Out-Gridview -Title 'Select INF Files to Estimate and Remove' -PassThru

    #$a = Get-ChildItem -Path "$Path" -Recurse $Files -File | Select-Object -Property Directory,Name

    foreach ($i in $a) {

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

        if ( $DirName -eq $LastDirName ) {
            #We have already measured this directory
            #Write-Host "This Directory has already been calculated"

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