Deleting Agents

A few of you sent mail asking how to delete an agent using the Command Shell. There is a Cmdlet for uninstalling an agent called Uninstall-Agent. However, uninstalling an agent will not work if the computer the agent is hosted by is not available. To help address the issue I created a function to automate the process. Keep in mind this function should only be used to delete an agent hosted by a computer that no longer exists otherwise you should use Uninstall-Agent. 

# Summary
#     Delete an agent hosted by a non-existent computer.
# Params
#     AgentNames - An array of strings that contain the FQDN of agents to delete.
# Returns
#     None
function global:Delete-Agent([System.String[]] $agentNames)
 $NoAgentsErrorMsg = "`nNo agent names specified. Please specify the FQDN for each agent you want to delete.`n";

 if ($agentNames -eq $null)
  Write-Host $NoAgentsErrorMsg;

 $administration = (get-item .).ManagementGroup.GetAdministration();

 $agentManagedComputerType = [Microsoft.EnterpriseManagement.Administration.AgentManagedComputer];

 $genericListType = [System.Collections.Generic.List``1]
 $genericList = $genericListType.MakeGenericType($agentManagedComputerType)

 $agentList = new-object $genericList.FullName

 foreach ($agentName in $agentNames)
  $agent = Get-Agent | where {$_.PrincipalName -eq $agentName}
  if ($agent -eq $null)
   $msg =  "Agent '{0}' not found." -f $agentName;  
   Write-Host $msg;


 if ($agentList.Count -eq 0)
  Write-Host $NoAgentsErrorMsg;

 $genericReadOnlyCollectionType = [System.Collections.ObjectModel.ReadOnlyCollection``1]
 $genericReadOnlyCollection = $genericReadOnlyCollectionType.MakeGenericType($agentManagedComputerType)

 $agentReadOnlyCollection = new-object $genericReadOnlyCollection.FullName @(,$agentList);

 $msg = "`nDeleting {0} agents:`n" -f $agentReadOnlyCollection.Count;
 Write-Host $msg;
 foreach ($agent in $agentReadOnlyCollection)
  Write-Host $agent.PrincipalName;

Hope that helps and as always please post your questions and comments and I will make sure they are addressed in future posts.

Roger Sprague

Comments (6)

  1. snepal23 says:

    I am not good at using the command shell. What kind of changes do i have to make to run this script? Please help. Thanks.

    This is a great help.

  2. LookingForAnswers says:


    Is there a way to use the administration.DeleteAgentManagedComputer without first converting your list of computers into a genericlist? I checked out all my options with the built in cmdlets for the operations manager 2007 console and there is no, Delete-Agent, which i find kind of strange as they have the Uninstall_Agent command let like you said.

    If there is no other way than i guess that is what i should go with.

    Thank you,

  3. LookingForAnswers says:

    I think i have found my answer, the other cmdlet that i think might work is

    Remove-RemotelyManagedComputer -Computer (Enter Computer Name Here)

    You can add more computer names by putting a comma between each of the computer names you add to your list.

  4. Dritan says:

    We have an old SCOM 2007 SP1 deployment which I am looking to blow away to enable a clean install of R2 into the organisation – the current infrastructure was unmanaged and looks basically unhealthy, 148 servers in the console but nearly all are greyed out healthy/critical .

    When i try to do an agent uninstall from the admin console the task never finish, it’s still running, have started before christmas.

    Is there a powershell script or something I can run to remove the old agent from these servers?  

    I have build an new SCOM R2 server and would like to use it after all agent have been removed.




  5. Stanislav Zhelyazkov says:

    Hi team,

    Is there a script for SCOM 2007 that will remove orphaned object of computer. We had a lot of these. Agent was not unintsalled and was just deleted from the console but we still see these computers in gray in Windows computers view.

  6. David Arnold says:

    Will this work with SCOM 2012? I amam desperately in need of this functionality.

Skip to main content