Skip to content

Commit 71cb072

Browse files
Enhance Remove-PSModule.ps1 to improve module and command removal process with detailed logging and accurate uninstallation
1 parent 593f698 commit 71cb072

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

scripts/helpers/Remove-PSModule.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,25 @@
2121
)
2222

2323
if ($PSCmdlet.ShouldProcess('Target', "Remove module [$Name]")) {
24-
Write-Host "Removing module [$Name]"
25-
Get-Module -Name $Name -ListAvailable | Remove-Module -Force
26-
Get-PSResource -Name $Name | Uninstall-PSResource -SkipDependencyCheck
24+
Write-Host "[$Name] - Remove module"
25+
$importedModule = Get-Module -ListAvailable | Where-Object { $_.Name -eq $Name }
26+
Write-Host " - Found [$($importedModule.Count)] modules to remove"
27+
foreach ($module in $importedModule) {
28+
Write-Host " - Removing module [$($module.Name)]"
29+
$module | Remove-Module -Force
30+
}
31+
$commands = Get-ChildItem -Path Function: | Where-Object { $_.Source -eq $Name }
32+
Write-Host " - Found [$($commands.Count)] commands to remove"
33+
foreach ($command in $commands) {
34+
Write-Host " - Removing command [$($command.Name)]"
35+
$command | Remove-Item -Force
36+
}
37+
$installedModule = Get-InstalledPSResource | Where-Object { $_.Name -eq $Name }
38+
Write-Host " - Found [$($installedModule.Count)] installed modules to remove"
39+
foreach ($module in $installedModule) {
40+
Write-Host " - Uninstalling module [$($module.Name)]"
41+
$module | Uninstall-PSResource -SkipDependencyCheck
42+
}
43+
Get-Command -Module $Name | Format-Table -AutoSize
2744
}
2845
}

0 commit comments

Comments
 (0)