-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathUninstall-OldModules.ps1
More file actions
47 lines (43 loc) · 1.69 KB
/
Uninstall-OldModules.ps1
File metadata and controls
47 lines (43 loc) · 1.69 KB
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
<#
.SYNOPSIS
Uninstalls old module versions (ignoring old Windows PowerShell modules).
.FUNCTIONALITY
PowerShell Modules
.EXAMPLE
Uninstall-OldModules.ps1
Cleans up redundant old modules.
#>
#Requires -Version 3
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)] Param(
# Indicates the modules should be forced to uninstall.
[switch] $Force
)
if(Get-Command Uninstall-PSResource -ErrorAction Ignore)
{
Get-Module -ListAvailable |
Where-Object {$_.ModuleBase -notlike '*\WindowsPowerShell\*'} |
Group-Object Name |
Where-Object Count -gt 1 |
ForEach-Object {$_.Group |Sort-Object Version -Descending |Select-Object -Skip 1} |
Where-Object {$Force -or $PSCmdlet.ShouldProcess("$($_.Name) v$($_.Version)",'Uninstall-PSResource')} |
ForEach-Object {Uninstall-PSResource $_.Name -Version $_.Version -Confirm:$false}
}
elseif($PSVersionTable.PSVersion -lt [version]'6.0')
{
Get-Module -ListAvailable |
Group-Object Name |
Where-Object Count -gt 1 |
ForEach-Object {$_.Group |Sort-Object Version -Descending |Select-Object -Skip 1} |
Where-Object {$Force -or $PSCmdlet.ShouldProcess("$($_.Name) v$($_.Version)",'Uninstall-Module')} |
ForEach-Object {Uninstall-Module $_.Name -RequiredVersion $_.Version -Force:$Force}
}
else
{
Get-Module -ListAvailable |
Where-Object {$_.ModuleBase -notlike '*\WindowsPowerShell\*'} |
Group-Object Name |
Where-Object Count -gt 1 |
ForEach-Object {$_.Group |Sort-Object Version -Descending |Select-Object -Skip 1} |
Where-Object {$Force -or $PSCmdlet.ShouldProcess("$($_.Name) v$($_.Version)",'Uninstall-Module')} |
ForEach-Object {Uninstall-Module $_.Name -RequiredVersion $_.Version -Force:$Force}
}