-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-OldModuleVersions.ps1
100 lines (76 loc) · 4.16 KB
/
Remove-OldModuleVersions.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
99
100
<#PSScriptInfo
.VERSION 1.0.0
.GUID 0c54c8cd-f696-4177-9f02-33cda299da78
.AUTHOR Tigran TIKSN Torosyan
.COMPANYNAME
.COPYRIGHT Copyright © Tigran TIKSN Torosyan
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.PRIVATEDATA
#>
#Requires -Module PSFramework
<#
.DESCRIPTION
Removes Old Module Versions
#>
[CmdletBinding()]
Param()
Write-PSFMessage -Level Debug -Message 'Looking up for all modules'
$allModules = Get-Module -ListAvailable
$modulePaths = $env:PSModulePath -split ';'
foreach ($modulePath in $modulePaths) {
Write-PSFMessage -Level Verbose -Message "Looking up for module path $modulePath"
if (Test-Path -Path $modulePath) {
$singleModulePaths = Get-ChildItem -Path $modulePath -Directory
foreach ($singleModulePath in $singleModulePaths) {
$singleModuleName = $singleModulePath.Name
Write-PSFMessage -Level Verbose -Message "Looking up for '$singleModuleName' module path $singleModulePath"
if (Test-Path -Path (Join-Path -Path $singleModulePath -ChildPath "$singleModuleName.psd1")) {
Write-PSFMessage -Level Verbose -Message "'$singleModuleName' has no versions"
}
else {
$singleModuleVersionPaths = Get-ChildItem -Path $singleModulePath -Directory
$singleModuleVersionList = @()
foreach ($singleModuleVersionPath in $singleModuleVersionPaths) {
$singleModuleVersion = [version]$singleModuleVersionPath.Name
Write-PSFMessage -Level Verbose -Message "Looking up for '$singleModuleName' module '$singleModuleVersion' version path $singleModuleVersionPath"
$singleModuleVersionList += ([PSCustomObject]@{
Name = $singleModuleName
Version = $singleModuleVersion
Path = $singleModuleVersionPath
})
}
if ($singleModuleVersionList.Count -gt 0) {
$singleModuleLatestVersion = ($singleModuleVersionList | Sort-Object -Property Version -Descending | Select-Object -First 1).Version
$singleModuleOldestVersionList = $singleModuleVersionList | Where-Object { $PSItem.Version -lt $singleModuleLatestVersion } | Sort-Object -Property Version
foreach ($singleModuleOldestVersion in $singleModuleOldestVersionList) {
Write-PSFMessage -Level Verbose -Message "Examining '$singleModuleName' module '$($singleModuleOldestVersion.Version)' version path $($singleModuleOldestVersion.Path)"
$isRequiredModule = $false
foreach ($givenModule in $allModules) {
foreach ($givenRequiredModule in $givenModule.RequiredModules) {
if (($givenRequiredModule.Name -eq $singleModuleOldestVersion.Name) -and ($givenRequiredModule.Version -eq $singleModuleOldestVersion.Version)) {
$isRequiredModule = $true
Write-PSFMessage -Level Verbose -Message "'$singleModuleName' module '$($singleModuleOldestVersion.Version)' version is required by '$($givenModule.Name)' module '$($givenModule.Version)' version"
}
}
}
if (-not $isRequiredModule) {
Write-PSFMessage -Level Verbose -Message "Deleting '$singleModuleName' module '$($singleModuleOldestVersion.Version)' version from '$($singleModuleOldestVersion.Path)' path"
Remove-Item -Path ($singleModuleOldestVersion.Path) -Recurse -Force
Write-PSFMessage -Level Important -Message "Deleted '$singleModuleName' module '$($singleModuleOldestVersion.Version)' version from '$($singleModuleOldestVersion.Path)' path"
}
}
}
}
}
}
else {
Write-PSFMessage -Level Warning -Message "Module path $modulePath does not exist"
}
}