Automating the SCOM 2012 R2 agent deletion using PowerShell

Automating the SCOM 2012 R2 agent deletion using PowerShell

In my last project I was given the task to automate the deletion of SCOM agents (both UNIX/Linux and Windows SCOM agents) using a PowerShell script, which then would be executed as part of the deprovisioning process of a specific virtual machine. This was necessary, because after decommissioning virtual machines their associated monitored objects remained in the SCOM database and were shown respectively in the console.

In addition to this, while answering questions in the Microsoft newsgroups, I also noticed that this question has been asked frequently. So I decided to write a short blog post on the topic, sharing my experience.

While gathering the requirements I have identified the need of two separate scripts – one serving the process of deprovisioning UNIX/Linux based virtual machines and one that had to be executed, while deprovisioning Windows managed systems. The reason for this was the fact that there were separate PowerShell cmdlets when it comes to handling the Windows and UNIX/Linux SCOM agents.

So I started with Linux agents and was pretty delighted to find out that there is a PowerShell cmdlet, which covers this exact requirement:


So using this information I was able to prepare the script for deprovisioning the Linux based agents in a pretty quick and easy manner. The end version of it looks like this:

Import-Module OperationsManager
Try {
$myAgent = Get-SCXAgent -Name $Name
Remove-SCXAgent -Agent $myAgent
 Catch {

You can find more information in regards to the UNIX/Linux PowerShell cmdlets here:

Operations Manager Cmdlets for UNIX and Linux

After completing the Linux part I decided to handle the automated deprovisioning of Windows based SCOM agents. So the first thing I did was to examine the Operations Manager Cmdlet reference on the Technet. I was a bit surprised to find out that there is only the possibility to repair or uninstall a Windows based SCOM agent, using PowerShell cmdlets. Deleting one was not possible using a single cmdlet.

Before starting writing a script I decided to check for solution in the SCOM community and was not surprised to find one from the first attempt. A Microsoft MVP (David Allen) has already wrote a script and published it on his page (thank you for your contribution, David):

SCOM 2012 R2 – Delete Agent using PowerShell¨

It was during the first test, when I discovered a small drawback of the script – there were still some leftovers in the “Monitoring\Windows Computer” section in the SCOM console, although the agents were already deleted and were not present in the “Administration\Agent Managed” pane in the console.

My observation was confirmed also by the comments under David’s original post, where he also suggested a working solution to remove the orphaned objects – running the “Remove-SCOMDisabledClassInstance” cmdlet (This operation may cause significant activity in the operational database, depending on your environment):


The cmdlet was able to remove the orphaned class instances and their relationships, but couldn’t be used for automation purposes. The reason for this was that I lacked a way to bypass the confirmation prompt:

**WARNING** This operation WILL result in class instances and relationships being permanently deleted.  This operation is irreversible and will likely result in a significant amount of activity in the operational database.  Are you sure you want to do this? [Y/N]

No matter what preference variables or error action do you use you cannot bypass this prompt. So leveraging the SCOM SDK was the only answer to this. Fortunately the Microsoft System Center community is strong and I haven’t had to look long for a solution. Actually I was able to find one from my first research on the topic. I came across an article, written in 2012 by Daniele Grandini, who suggested using the „$mg.EntityObjects.DeleteDisabledObjects()” method:

How to schedule the Remove-SCOMDisabledClassInstance comdlet

It was also mentioned in a Microsoft Technet Blog post, referring back to the original article, Daniele wrote:

Remove Disabled Monitoring Objects from SCOM 2012

The only thing I had to do was to put the parts together and add „$mg.EntityObjects.DeleteDisabledObjects()” to David’s script, leading to the end version of this code. So that others can quickly sort this out, I have uploaded David’s script with the small adjustment (Daniele’s suggestion) on the Microsoft Technet gallery, where it is available for you to download:

PowerShell script for automating SCOM Windows agent deletion

What do you need to do? Just make sure your run the script with the following parameters:

-Management Server <FQDN of your SCOM Management Server>” and

-AgentFQDN <FQDN of the SCOM Agent to be deleted>

and you will have a fully automated way of removing the stale SCOM Windows agents from your Management Group.

2 thoughts on “Automating the SCOM 2012 R2 agent deletion using PowerShell
  1. Birdal says:

    is it poosible to delete / uninstall the agants of an offline Computer?
    Best Regards

    • Stoyan Chalakov
      Stoyan Chalakov says:

      Hi Birdal,

      you can do this on the SCOM side of course, but still the computer will have an agent installed and you will have to take care of it somehow.


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.