title: Delete User from AD Group
body: <p>I am trying to delete a user from an Active Directory group via code. Getting the helpful error of: </p>
  
  <blockquote>
    <p>Exception has been thrown by the targe
    of an invocation"</p>
    
    <p>Trace: Stack Trace: at
    System.DirectoryServices.DirectoryEntry.Invoke(String
    methodName, Object[] args) at
    Active_Directory.RemoveUserFromGroup(String
    sInUserName, String sInGroupName) in
    C:\Documents and Settings\user\My
    Documents\Visual Studio
    2010\WebSites\appname\App_Code\Common\Active_Directory.vb:line
    192</p>
  </blockquote>
  
  <p>here is my function: </p>
  
  <p>Check out the Invoke Line: oGroup.Invoke("Remove", New Object() {oUser.Path}) </p>
  
  <pre><code>Public Shared Sub RemoveUserFromGroup(ByVal sInUserName As String _
                                        , ByVal sInGroupName As String)
      Dim entry1 As DirectoryEntry
      Dim de As DirectoryEntry
      Dim deSearch As DirectorySearcher
      Dim results As SearchResult
      Dim comeon As String
      Dim oUser As DirectoryEntry
      Dim oGroup As DirectoryEntry
      Dim sr As SearchResult
  
      Try
  
          entry1 = New DirectoryEntry("LDAP://rootDSE")
          comeon = entry1.Properties("DefaultNamingContext").Item(0)
          de = New DirectoryEntry("LDAP://" &amp; comeon)
  
          deSearch = New DirectorySearcher()
          deSearch.SearchRoot = de
          deSearch.Filter = "(sAMAccountName=" + sInUserName + ")"
          deSearch.PropertiesToLoad.Add("cn")
          sr = deSearch.FindOne()
  
          If sr Is Nothing Then
              oUser = Nothing
          Else
              oUser = sr.GetDirectoryEntry()
          End If
  
          deSearch.Dispose()
          deSearch = Nothing
          sr = Nothing
  
          If Not (oUser Is Nothing) Then
  
              deSearch = New DirectorySearcher()
              deSearch.SearchRoot = de
              deSearch.Filter = "(&amp;(objectClass=group) (CN=" &amp; sInGroupName &amp; "))"
              deSearch.SearchScope = SearchScope.Subtree
  
              results = deSearch.FindOne()
  
              If results IsNot Nothing Then
  
                  oGroup = results.GetDirectoryEntry()
  
                  Try
  
                      oGroup.Invoke("Remove", New Object() {oUser.Path})
                      oGroup.CommitChanges()
                      oGroup.Close()
  
                  Catch ex As Exception
                      Dim s As String
                      s = ex.ToString
                      s = ""
                  End Try
  
              End If
  
              entry1.Dispose()
              de.Dispose()
  
              entry1 = Nothing
              de = Nothing
              deSearch = Nothing
              results = Nothing
  
          End If
  
          oUser.Close()
  
      Catch ex As Exception
  
          Dim myerror As New MyError
          myerror.showMeTheError(ex)
  
      End Try
  
  End Sub
  </code></pre>