-
Notifications
You must be signed in to change notification settings - Fork 28
/
Uninstall-PowerShellModuleAndDependencies.ps1
56 lines (46 loc) · 1.66 KB
/
Uninstall-PowerShellModuleAndDependencies.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
<#
do{
$mod=Get-InstalledModule Vmware* -ErrorAction SilentlyContinue
$mod | % {$_.Name
$_ | Uninstall-Module}}until($(Get-InstalledModule Vmware* -ErrorAction SilentlyContinue) -eq $null)
#>
function Uninstall-PowerShellModuleAndDependencies {
param (
[Parameter(Mandatory = $true)]
[string]$Module
)
[System.Collections.Generic.List[String]] $script:modulesOrDependencies = @()
function Get-RecursivePowerShellDepencies {
param (
[Parameter(Mandatory = $true)]
[string]$Module
)
$target = Find-Module $Module
if ($target.Dependencies.count -eq 0) {
if ($script:modulesOrDependencies -notcontains $target.Name) {
$script:modulesOrDependencies.Add($target.name)
}
}
else {
$target.Dependencies | ForEach-Object {
# add current dependency
if ($script:modulesOrDependencies -notcontains $target.Name) {
$script:modulesOrDependencies.Add($target.name)
}
# search for dependencies of current dependency
Get-RecursivePowerShellDepencies -Module $_.name
}
}
}
Write-Host "$module - Creating list of dependencies" -ForegroundColor Cyan
Get-RecursivePowerShellDepencies -Module $Module
foreach ($mod in $modulesOrDependencies) {
Write-Host "$mod - Uninstalling" -ForegroundColor Cyan
try {
Uninstall-Module -Name $mod -Force -ErrorAction Stop
}
catch {
Write-Warning "modulesOrDependency $($_.Exception.Message)"
}
}
}