Skip to content
HOME / AZURE / REMOVE OLD POWERSHELL MODULES 3 years AGO

Azure

Remove Old PowerShell Modules

Remove Old PowerShell Modules

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”.

Arnav Sharma
Arnav Sharma Microsoft MVPMCT
Microsoft Certified Trainer · Cloud · Cybersecurity · AI

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

KEEP READING

Machine Identities in Azure

Last Updated on June 23, 2026 by Arnav Sharma As an architect who has spent years helping organisations rebuild their Azure identity…

2026.06.23 · 11 MIN READ

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.