Last Updated on August 11, 2025 by Arnav Sharma
PowerShell modules are self-contained scripts or functions that extend the functionality of PowerShell. They provide a way to package and distribute PowerShell code, making it easier to reuse and share. However, over time, you may accumulate various versions of modules, which can lead to confusion and potential issues. In this article, we will explore how to remove old PowerShell modules and best practices for managing them.
Here’s the Script to check for old modules, remove them and install new ones:
# Get a list of all installed modules
$installedModules = Get-InstalledModule
$totalModules = $installedModules.Count
$currentModule = 0
foreach ($module in $installedModules) {
$currentModule++
# Get all versions of the module
$allVersions = Get-InstalledModule -Name $module.Name -AllVersions
# If there's more than one version installed
if ($allVersions.Count -gt 1) {
# Sort by version and select all but the latest version
$oldVersions = $allVersions | Sort-Object {[Version]$_} | Select-Object -First ($allVersions.Count - 1)
# Uninstall old versions
foreach ($oldVersion in $oldVersions) {
Write-Host "Uninstalling $($oldVersion.Name) version $($oldVersion.Version)..."
Uninstall-Module -Name $oldVersion.Name -RequiredVersion $oldVersion.Version -Force
}
}
# Install the latest version
Write-Host "Installing latest version of $($module.Name)..."
Install-Module -Name $module.Name -Force -AllowClobber -SkipPublisherCheck
# Update progress bar
Write-Progress -PercentComplete (($currentModule / $totalModules) * 100) -Status "Processing Modules" -Activity "Processed $currentModule of $totalModules"
}
Write-Progress -Completed
Write-Host "Script completed!"
What is a PowerShell module?
Introduction to PowerShell modules: A PowerShell module is a collection of cmdlets, functions, and scripts that are grouped together for easy management and distribution. Modules are designed to enhance the capabilities of PowerShell, allowing you to perform specific tasks more efficiently.
Why should you remove old PowerShell modules? Removing old PowerShell modules is essential for maintaining a clean and streamlined environment. It helps prevent conflicts between different versions of the same module, reduces clutter in your module list, and ensures you are using the latest and most stable versions of modules.
How to uninstall a PowerShell module?
Using the Uninstall-Module command: The Uninstall-Module command is the primary way to remove a PowerShell module. It allows you to specify the module to remove and provides options to control the removal process.
Specifying the module name to remove: To uninstall a specific module, you need to provide its name using the -Name parameter. For example, to uninstall a module named “ExampleModule”, you would run the command “Uninstall-Module -Name ExampleModule”.
Removing all versions of a module: If you want to remove all versions of a module, you can use the -AllVersions parameter. This ensures that every version of the specified module is uninstalled.
Using the -Force parameter
Understanding the -Force parameter: The -Force parameter is used to forcibly uninstall a module, even if it is in use or required by other modules or scripts. It overrides any warnings or restrictions and uninstalls the module.
The impact of using the -Force parameter: While the -Force parameter can be handy in certain situations, it should be used with caution. Removing a module forcefully may break dependencies and cause unexpected behavior in PowerShell sessions or scripts.
Scenarios where using the -Force parameter is recommended: The -Force parameter is commonly used when you are sure about the consequences of removing a module forcefully or when troubleshooting module-related issues that cannot be resolved otherwise.
How to retrieve a list of installed PowerShell modules?
Using the Get-InstalledModule command: The Get-InstalledModule command allows you to retrieve a list of installed PowerShell modules on your system. It provides information such as the module name, version, and installed location.
Filtering the module list based on the version: If you want to filter the module list based on a specific version, you can use the -RequiredVersion parameter. This helps you identify modules with older versions that can be removed.
Exporting the module list to a file: To save the module list for reference or analysis, you can export it to a file using the > (output to file) operator. For example, “Get-InstalledModule > modulelist.txt” exports the list to a file named “modulelist.txt”.
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
A PowerShell module is a collection of cmdlets, functions, and scripts grouped together to enhance PowerShell's capabilities and make code reusable and shareable. Over time, you may accumulate multiple versions of modules, which can cause confusion, conflicts, and potential issues, making it important to manage and remove old versions regularly.
You can use the Uninstall-Module command with the -Name parameter to remove a specific module. For example, running 'Uninstall-Module -Name ExampleModule' will uninstall that module. If you want to remove all versions of a module, you can add the -AllVersions parameter to the command.
The -Force parameter forcibly uninstalls a module even if it's in use or required by other modules or scripts, overriding any warnings or restrictions. However, it should be used with caution because forcefully removing a module may break dependencies and cause unexpected behavior in PowerShell sessions or scripts.
You can use the Get-InstalledModule command to retrieve a list of all installed PowerShell modules along with their names, versions, and locations. You can also filter this list by version using the -RequiredVersion parameter or export it to a file using the output operator (>) for reference and analysis.
The script automates the process of managing PowerShell modules by identifying all installed modules, checking for multiple versions, uninstalling old versions while keeping only the latest one, and reinstalling the current latest version. It uses the -Force and -AllowClobber parameters to ensure smooth operation and provides progress updates throughout the process.