SharePoint (2003 thru Online): Uninstall All Powershell Modules

Wednesday, September 25, 2024

Uninstall All Powershell Modules

The script uses the Get-InstalledModule cmdlet to identify all installed PowerShell modules. It then employs the -match operator to find modules whose names include "Microsoft". For these identified modules, the script executes the Uninstall-Module cmdlet with the -Force option to guarantee their removal. Essentially, the script is designed to remove any PowerShell module that contains "Microsoft" in its name. 

Another variable -notmatch is targeted to remove any PowerShell module that does not contain "Microsoft" in its name. 

Additionally, there's a variable, $excludedModules, which is an array holding the names of modules that you wish to keep. By using the condition -and $excludedModules -notcontains $_.Name, the script ensures it only removes "Microsoft" named modules that are not specified in the exclusion list. 

You can customize this list by substituting "ModuleName1" and "ModuleName2" with the names of the modules you'd like to exclude from being uninstalled.

# List of modules to exclude

$excludedModules = @("ModuleName1", "ModuleName2")

Get-InstalledModule | ForEach-Object {

   # Use this line uninstalls all PowerShell modules that have "Microsoft" in their name.

   if ($_.Name -match "Microsoft") {

    # Use this line uninstalls all PowerShell modules that do not have "Microsoft" in their name.

    #if ($_.Name -notmatch "Microsoft") {

        # Use this line uninstalls all PowerShell modules that have "Microsoft" in their name and exclude above List of modules.

        #if ($_.Name -match "Microsoft" -and $excludedModules -notcontains $_.Name) {

         Uninstall-Module -Name $_.Name -Force

}

 }


NOTE: In PowerShell, the # symbol is used to add comments. Comments are lines or parts of lines that are not executed as part of the script. They are used to provide explanations or notes about the code, making it easier to understand.

No comments:

Post a Comment