Remove PowerShell modules by name
Let’s say you installed a lot of PowerShell modules, and maybe several different versions. Cleaning this up or upgrading to the latest version is a terrible job.
Luckily you can use PowerShell to fix this for you.
Remove all installed Microsoft.Graph modules
# Remove Microsoft.Graph* but not Microsoft.Graph.Authentication (dependency mess....)
$Modules = Get-Module Microsoft.Graph* -ListAvailable | Where {$_.Name -ne "Microsoft.Graph.Authentication"} | Select-Object Name -Unique
Foreach ($Module in $Modules)
{
$ModuleName = $Module.Name
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions)
{
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
}
}
#Uninstall Microsoft.Graph.Authentication
$ModuleName = "Microsoft.Graph.Authentication"
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions)
{
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
}
Remove all installed modules starting with
$modulesToRemove = "Azure*"
$Modules = Get-Module $modulesToRemove -ListAvailable | Select-Object Name -Unique
Foreach ($Module in $Modules)
{
$ModuleName = $Module.Name
$Versions = Get-Module $ModuleName -ListAvailable
Foreach ($Version in $Versions)
{
$ModuleVersion = $Version.Version
Write-Host "Uninstall-Module $ModuleName $ModuleVersion"
Uninstall-Module $ModuleName -RequiredVersion $ModuleVersion
}
}