Last Updated on May 27, 2024 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”.
FAQ – Using PowerShell Uninstall
Q: What is PowerShellGet?
A: PowerShellGet is a module in Windows PowerShell that enables you to manage modules, DSC resources, and scripts in a simple, unified way.
Q: How do I remove old PowerShell modules?
A: You can remove old PowerShell modules using the Remove-Module cmdlet.
Q: How can I view the available versions of a PowerShell module?
A: You can use the Get-Module command with the -ListAvailable parameter to view the available versions of a PowerShell module.
Q: Can I remove multiple versions of a module using PowerShell?
A: Yes, you can remove multiple versions of a module using the Remove-Module cmdlet with the -Name parameter followed by the name of the module and the -Force parameter.
Q: How do I remove a PowerShell module from the local computer?
A: You can remove a PowerShell module from the local computer using the Uninstall-Module cmdlet with the -Name parameter followed by the name of the module.
Q: Can I remove a module using a wildcard pattern?
A: Yes, you can remove a module using a wildcard pattern by specifying the pattern in the -Name parameter of the Remove-Module cmdlet.
Q: How do I remove a specific version of a module using PowerShell?
A: You can specify the module version in the -RequiredVersion parameter of the Remove-Module cmdlet to remove a specific version of a module.
Q: What is the -Verbose parameter used for when removing a module?
A: The -Verbose parameter is used to display detailed information about the removal process.
Q: Can I remove a module without being prompted for confirmation?
A: Yes, you can use the -Confirm parameter with a value of $false to remove a module without being prompted for confirmation.
Q: Can I use the -WhatIf parameter to see the changes that will be made when removing a module?
A: Yes, you can use the -WhatIf parameter with the Remove-Module cmdlet to see the changes that will be made without actually removing the module.
keywords: Delete and uninstall the module string using microsoft module installed powershellget module in github pipeline prerelease requiredversion verbose ps answer you’re looking
Hi,
here is the output I get using your script, which I named update.ps1 and is on my desktop:
Installing latest version of PSWindowsUpdate…
Attempted to divide by zero.
At C:\users\Username\desktop\update.ps1:30 char:5
+ Write-Progress -PercentComplete (($currentModule / $totalModules) …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
cmdlet Write-Progress at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Activity:
1.Regarding PSWindowsUpdate, I don’t know why your script is attempting to update this module when it is up to date. I did actually manually update PSWindowsUpdate from v2.2.1.4 to v2.2.1.5 just before I ran your script, then uninstalled 2.2.1.4 Does this have something to do with why it’s trying to update it?
2. why is it trying to divide by zero?
3. Why does it want me to supply a parameter for activity? Does this parameter not automatically show the percent progress of the install? Thus no user input should be required? If I provide random input, the script ends.
Thanks.
1. The script tries to update every module, regardless of whether it’s already up to date. This happens because Install-Module with -Force will re-install the module even if it is the latest version. To avoid this, you could check if the latest version installed is the same as the version available for installation before attempting an update.
2. This error suggests that $totalModules might be zero at the time it’s used in the division for calculating the percent complete. This can happen if Get-InstalledModule does not return any modules, possibly due to running the script in an environment where no modules are installed, or a permissions issue preventing the script from reading installed modules. To troubleshoot, you can add a check to ensure $totalModules is greater than zero before the loop starts.
3. The error you’re seeing with Write-Progress is likely due to $totalModules being zero, which makes the division undefined, and PowerShell might then halt to request user input for the subsequent Write-Progress. Normally, the Activity parameter should indeed be automatically populated by the script, and no manual input should be required.
Try below:
$installedModules = Get-InstalledModule
$totalModules = $installedModules.Count
if ($totalModules -eq 0) {
Write-Host “No modules installed.”
exit
}
$currentModule = 0
foreach ($module in $installedModules) {
$currentModule++
$allVersions = Get-InstalledModule -Name $module.Name -AllVersions
if ($allVersions.Count -gt 1) {
$oldVersions = $allVersions | Sort-Object {[Version]$_} | Select-Object -First ($allVersions.Count – 1)
foreach ($oldVersion in $oldVersions) {
Write-Host “Uninstalling $($oldVersion.Name) version $($oldVersion.Version)…”
Uninstall-Module -Name $oldVersion.Name -RequiredVersion $oldVersion.Version -Force
}
}
# Check for the latest available version before installing
$latestAvailable = Find-Module -Name $module.Name | Select-Object -ExpandProperty Version
$latestInstalled = $allVersions | Sort-Object {[Version]$_} | Select-Object -Last 1 -ExpandProperty Version
if ([Version]$latestInstalled -lt [Version]$latestAvailable) {
Write-Host “Installing latest version of $($module.Name)…”
Install-Module -Name $module.Name -Force -AllowClobber -SkipPublisherCheck
} else {
Write-Host “$($module.Name) is already up to date.”
}
Write-Progress -PercentComplete (($currentModule / $totalModules) * 100) -Status “Processing Modules” -Activity “Processed $currentModule of $totalModules”
}
Write-Progress -Completed
Write-Host “Script completed!”