Find-OSDriverINFs.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
<#
.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/18/2018
    VERSION: 1.1.0.2
#>


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