Summary of Remove-SCOMDisabledClassInstance
In System Center Operations Manager (SCOM), there are instances where certain class objects become disabled and need to be removed to maintain a healthy management environment. The Remove-SCOMDisabledClassInstance function automates the process of cleaning up these disabled class instances. This is particularly useful for administrators who manage large SCOM environments and need to regularly clean up these objects to ensure optimal performance and management.
Why It’s Helpful to Add This Custom Function
The default implementation of Remove-SCOMDisabledClassInstance requires user input to confirm the removal of disabled objects, which can be cumbersome and inefficient, especially in automated scripts or batch operations. By defining a custom function in your profile, you do not modify the original implementation but rather create a duplicate that takes precedence over the original. This means that whenever you call Remove-SCOMDisabledClassInstance
in your PowerShell session, your custom version will be executed instead of the original. However, the original function remains available and can still be run if needed. To execute the original function, you can use the fully qualified name or temporarily rename your custom function. This approach allows you to streamline the process while retaining the ability to access the original functionality.

The original cmdlet requires confirmation.
What is the AllUsersAllHosts Profile?
PowerShell profiles are scripts that run when PowerShell starts. They can be used to customize the environment by defining functions, aliases, variables, and more. There are different types of profiles that apply to different scopes:
- Current User, Current Host: This profile applies to the current user and the current PowerShell host (e.g., PowerShell console, ISE).
- Current User, All Hosts: This profile applies to the current user across all PowerShell hosts.
- All Users, Current Host: This profile applies to all users of the system but only the current PowerShell host.
- All Users, All Hosts: This profile applies to all users of the system across all PowerShell hosts.
The AllUsersAllHosts profile is located at $PSHOME\Profile.ps1
, where $PSHOME
is the directory where PowerShell is installed. This profile is executed for all users and all hosts, making it an ideal place to define functions and configurations that should be globally available on the system.
Adding the Function to Your PowerShell Profile
To make the Remove-SCOMDisabledClassInstance function readily available in any PowerShell session, we can add it to the AllUsersAllHosts profile. This ensures that the function is available to all users on the system and in all PowerShell hosts, including the Console and OperationsManager PowerShell module.
PowerShell Code
Here’s the PowerShell script to define the Remove-SCOMDisabledClassInstance
function and add it to the AllUsersAllHosts profile. Only run this code once per server/workstation, wherever your OperationsManager PowerShell module exists:
# This will add the custom function to the AllUsersAllHosts profile.
# Only run this ONCE!
# Define the function code
$functionCode = @'
function Remove-SCOMDisabledClassInstance {
# Begin
[Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.Core, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") | Out-Null
[Reflection.Assembly]::Load("Microsoft.EnterpriseManagement.OperationsManager, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35") | Out-Null
$mg = [Microsoft.EnterpriseManagement.ManagementGroup]::Connect("localhost")
$mg.EntityObjects.DeleteDisabledObjects()
# End
}
'@
# Path to the AllUsersAllHosts profile
$profilePath = "$($PSHOME)\Profile.ps1"
# Ensure the profile file exists
if (-not (Test-Path -Path $profilePath)) {
# Create an empty profile file if it does not exist
New-Item -Path $profilePath -ItemType File -Force
}
# Add the function to the profile
Add-Content -Path $profilePath -Value $functionCode
Write-Host "Function Remove-SCOMDisabledClassInstance added to the AllUsersAllHosts profile." -ForegroundColor Cyan
Explanation
- Defining the Function:
The function is defined within a here-string (@' ... '@
) to preserve the formatting and allow for multi-line input. This function loads the necessary assemblies for the SCOM SDK, connects to the local SCOM management group, and deletes any disabled objects without requiring user confirmation. - Determining the Profile Path:
The script sets the path to the AllUsersAllHosts profile, which is located in the PowerShell home directory. This is done using the variable$PSHOME
. - Ensuring the Profile File Exists:
Before adding the function to the profile, the script checks if the profile file exists. If it doesn’t, the script creates an empty profile file to ensure that the function can be appended. - Adding the Function to the Profile:
TheAdd-Content
cmdlet is used to append the function definition to the profile file. This ensures that the function is available in all future PowerShell sessions for all users on the system. - Confirmation Message:
A confirmation message is displayed to inform the user that the function has been successfully added to the profile.
How to Use the Script
- Open a PowerShell window with administrative privileges.
- Copy and paste the script into the PowerShell window.
- Execute the script.
By following these steps, you’ll add the Remove-SCOMDisabledClassInstance function to the AllUsersAllHosts profile, making it easily accessible for regular maintenance of your SCOM environment.
This small addition to your PowerShell profile can save time and help keep your SCOM environment clean and efficient. By automating the confirmation process, you can seamlessly integrate this function into your scripts and batch operations, significantly enhancing your productivity. Happy scripting!