Sometimes you might need to delete some management packs because the underlying monitored technology is no longer used in your organization or it simply became deprecated. Removing a management pack is not straightforward and you might have references to this management pack present in other management packs.
In this particular case I had to remove some Exchange 2010 management packs that had various references through some other management packs.
When you open a management pack you will see the dependencies in the lower part.

Until you remove the references from those dependent management packs, you cannot remove the management pack that you need to remove.
For the unsealed management packs, in order to remove the dependencies, you must export them, open them in an editor, I recommended Notepad++, and based on the alias in the reference, search for all the elements it’s referencing and delete them.
Sometimes, those dependent management packs may contain only the alias and no additional elements, being basically empty of other elements.
Note: If you have custom sealed management packs, you would need to export, edit, seal and import back into SCOM. For other sealed management packs, from Microsoft for example, you would need to simply delete the referenced management packs first.
We would need to first export the dependent management packs. You could go through the console and do it one by one or we can use PowerShell to do this. Here is an example:
Import-Module OperationsManager
$Credentials = Get-Credential -Message "Please enter administrative credentials for accessing the file information on the SCOM servers" -UserName "username"
$SCOMServer = "YourSCOMServer"
New-SCOMManagementGroupConnection -Credential $Credentials -ComputerName $SCOMServer -Verbose
$AllMP = Get-SCOMManagementPack
$MPs = $AllMP | ogv -PassThru -Title "Select the Management Packs that you want to remove. Any Dependencies will be exported automatically."
$Dependencies = foreach ($MP in $MPs){$AllMP | ? {$_.References.Values.Name -match $MP.Name}}
$Dependencies | sort Displayname
Write-host -ForegroundColor Yellow "Enter the path to an existing folder where the Management Packs will be exported: " -NoNewline
$Pathinput = Read-Host
Try{$testresp = Get-ChildItem $Pathinput -ErrorAction Stop}catch{$_.Exception.Message| Write-Host -ForegroundColor Red}
$Dependencies | Export-SCOMmanagementpack -Path $Pathinput -Verbose
The code above allows you to export any dependencies to a location where you can do the cleaning process.
Then you can begin to remove the referenced elements (monitors, views, etc.) from the management packs and seal them if sealing is required. Next, you need to import back the management packs into SCOM.
Sometimes when you do that you might receive a some errors relating to display strings that are referencing various monitors/rules, pointing to the management pack you wish to remove. Normally you would have to find the monitor that was referencing the management pack, get the ID and search for the display string of that ID. And repeat that process many times. The problem is that there can be so many of them that it becomes unpractical.
Here is one way you could to do it. First you need to copy the error output into a text file.
Then you can use the following code to read the document, match the element IDs, remove them and save the file.
$ErrorList = gc C:\temp\Regex\Errors.txt
[regex]$regex = '(OverrideForRule[A-Z1-z0-9]*)|(OverrideForMonitorUIGeneratedMonitor[A-Z1-z0-9]*)'
$TargetElements = $ErrorList | Select-String -Pattern $regex | % {$_.Matches.Value}
[xml]$XML = gc C:\Temp\Regex\OTAMECSCustom.xml
#Show Matches
foreach ($TargetElement in $TargetElements){
$OneElement = $XML | Select-Xml -XPath "//DisplayStrings/DisplayString[@ElementID='$TargetElement']"
$OneElement}
#remove Nodes
foreach ($TargetElement in $TargetElements){
$OneElement = $XML.SelectSingleNode("//DisplayStrings/DisplayString[@ElementID='$TargetElement']")
$OneElement.ParentNode.RemoveChild($OneElement)
}
$XML.Save('C:\temp\OutputMP.xml')
After this you should be able to import the management pack without errors.
