Expand-OSDriver.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
<#
.LINK
    https://www.osdeploy.com/psmodule/osdrivers/
.SYNOPSIS
    Option to expand compressed drivers and to cleanup Nvidia Drivers
.DESCRIPTION
    Optionally expands compressed drivers. Optionally removes Nvidia Driver junk
.PARAMETER Path
    Directory containing Drivers
.PARAMETER ExpandCompressedFiles
    Expands compressed files *.bi,*.cf,*.cp,*.dl,*.ex,*.hl,*.pd,*.sy,*.tv,*.xm
.PARAMETER NvidiaCleanup
    Removes directories named Display.NView, Display.Optimus, Display.Update, DisplayDriverCrashAnalyzer, GFExperience, GFExperience.NvStreamSrv, MSVCRT, nodejs, NV3DVision, NvBackend, NvCamera, NvContainer, NVI2, NvTelemetry, NVWMI, PhysX, ShadowPlay, Update.Core
.EXAMPLE
    Expand-OSDriver -Path C:\Temp\Nvidia -ExpandCompressedFiles -NvidiaCleanup
.NOTES
    NAME: Expand-OSDriver.ps1
    AUTHOR: David Segura, david@segura.org
    BLOG: http://www.osdeploy.com
    CREATED: 02/18/2018
    VERSION: 1.1.0.2
#>


function Expand-OSDriver
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$Path,
        [switch]$ExpandCompressedFiles,
        [switch]$NvidiaCleanup
    )

    Write-Host "***** Calculating Path Size *****"
    $SizeStart = "{0:N2} GB" -f ((Get-ChildItem $Path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
    
    if ($ExpandCompressedFiles) {
        Write-Host "***** Expanding Files *****"
        Get-ChildItem -Path $Path -Include *.*_ -Recurse | ForEach ($_) {
            expand -r $_.FullName
            Remove-Item $_.FullName
        }
        
        #Rename Expanded Files
        Get-ChildItem -Path $Path -Include *.bi,*.cf,*.cp,*.dl,*.ex,*.hl,*.pd,*.sy,*.tv,*.xm -Recurse | ForEach ($_) {
            $NewName=$_.Name.Replace(".bi",".bin")
            $NewName=$NewName.Replace(".cf",".cfg")
            $NewName=$NewName.Replace(".cp",".cpl")
            $NewName=$NewName.Replace(".dl",".dll")
            $NewName=$NewName.Replace(".ex",".exe")
            $NewName=$NewName.Replace(".hl",".hlp")
            $NewName=$NewName.Replace(".pd",".pdf")
            $NewName=$NewName.Replace(".sy",".sys")
            $NewName=$NewName.Replace(".tv",".tvp")
            $NewName=$NewName.Replace(".xm",".xml")
           Rename-Item $_.FullName -NewName $NewName
        }
        
        Write-Host "***** Calculating Path Size *****"
        $SizeExpanded = "{0:N2} GB" -f ((Get-ChildItem $Path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
    }
    
    if ($NvidiaCleanup) {
        $Remove =     "Display.NView",
                    "Display.Optimus",
                    "Display.Update",
                    "DisplayDriverCrashAnalyzer",
                    "GFExperience",
                    "GFExperience.NvStreamSrv",
                    "MSVCRT",
                    "nodejs",
                    "NV3DVision",
                    "NvBackend",
                    "NvCamera",
                    "NvContainer",
                    "NVI2",
                    "NvTelemetry",
                    "NVWMI",
                    "PhysX",
                    "ShadowPlay",
                    "Update.Core"

        ForEach ($Dir in $Remove) {
            Write-Host "***** Removing Directories Named $Dir *****"
            Remove-Item -Path $Path -Include $Dir -Recurse
        }
        
        Write-Host "***** Calculating Path Size *****"
        $SizeDeJunked = "{0:N2} GB" -f ((Get-ChildItem $Path -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1GB)
    }
    
    Write-Host "Initial Size: " $SizeStart
    if ($ExpandCompressedFiles) {Write-Host "Expanded Size: " $SizeExpanded}
    if ($NvidiaCleanup) {Write-Host "De-Junked Size: " $SizeDeJunked}
}