Cleaning Up PowerShell Modules (Part 2)

by Jul 1, 2022

In part 1 we looked at removing PowerShell modules that were originally installed via “Install-Module”. You can as well remove PowerShell modules manually if you no longer need them. After all, they are just folders.

Here’s code that lists all available PowerShell modules and lets you choose which one to delete.

# folders where PowerShell looks for modules:
$paths = $env:PSModulePath -split ';'
# finding actual module folders
$modules = Get-ChildItem -Path $paths -Depth 0 -Directory | Sort-Object -Property Name

$modules | 
  Select-Object -Property Name, @{N='Parent';E={$_.Parent.FullName}}, FullName |
  Out-GridView -Title 'Select module(s) to permanently delete' -PassThru |
  Out-GridView -Title 'Do you REALLY want to remove the modules below? CTRL+A and OK to confirm' -PassThru |
  Remove-Item -Path { $_.FullName } -Recurse -Force -WhatIf # remove -WhatIf to actually delete (as always at own risk)

Note: you may need Administrator privileges if the module was installed in “AllUsers” scope.

Attention: when you remove a module, it will be permanently deleted from your hard drive. Make sure you know which cmdlets it shipped, and that you do not require them any longer.

 


Twitter This Tip! ReTweet this Tip!