To delete old PowerShell modules, identify modules with multiple installed versions and uninstall every version except the newest. The function below automates that cleanup for modules installed through PowerShellGet.
Close other PowerShell sessions and keep any older version required by production scripts or compatibility testing.
Remove old module versions
Function Remove-OldModules {
$latest = Get-InstalledModule
Foreach ($module in $latest) {
$AllVersionsOfModule = Get-InstalledModule -Name $module.Name -AllVersions
If ($AllVersionsOfModule.count -gt 1) {
Write-Host -f yellow "[-] Uninstalling old versions of $($module.Name) [latest is $( $module.Version)]" -Verbose
$AllVersionsOfModule | Where-Object {$_.Version -ne $module.Version} | Uninstall-Module
}
}
}
Preview installed versions first
Get-InstalledModule -AllVersions |
Sort-Object Name, Version |
Select-Object Name, Version, InstalledLocation
The function keeps the latest version and sends older versions to Uninstall-Module. A loaded module, a dependent module, or a module copied manually can prevent removal. Microsoft's Uninstall-Module documentation explains these restrictions.
Update and test the latest releases before cleanup using the PowerShell module update command.
How to delete old PowerShell modules safely
PowerShell can keep several versions of the same module side by side. This is useful for compatibility, but obsolete versions consume disk space and can make troubleshooting harder when different sessions load different releases. Before deleting anything, identify which version your scripts import and test the newest release in a non-production session.
Check module scope and installation path
Modules installed with -Scope CurrentUser normally live under the current profile, while all-user installations require administrative rights. The preview command shows InstalledLocation, allowing you to confirm both the version and scope. Do not manually remove folders from PSModulePath unless the module was originally copied there manually and its dependencies are understood.
Verify the cleanup
After the function finishes, run Get-InstalledModule -AllVersions again. Each processed module should retain its latest installed version. Open a new PowerShell session, import the modules used by your scripts, and run a basic functional test. If an older version is still present, check whether it is loaded, required by another module, installed under another user scope, or protected by file permissions.
Deleting old PowerShell modules does not update the remaining release. Use Update-Module first when you need a newer version, validate it, and only then remove superseded copies. Keeping a tested rollback package is sensible for critical automation.

Comments