Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1958398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:18:51+00:00 2026-05-17T08:18:51+00:00

I am trying to delete a user from an Active Directory group via code.

  • 0

I am trying to delete a user from an Active Directory group via code. Getting the helpful error of:

Exception has been thrown by the targe
of an invocation”

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

here is my function:

Check out the Invoke Line: oGroup.Invoke(“Remove”, New Object() {oUser.Path})

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://" & 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 = "(&(objectClass=group) (CN=" & sInGroupName & "))"
            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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T08:18:51+00:00Added an answer on May 17, 2026 at 8:18 am

    You seem to be doing it extremely complicated – unnecessarily so.

    Check out the Howto do almost everything in Active Directory CodeProject article – excellent stuff.

    Here’s the snippet needed to remove a user (given by his DN) from a group (also defined by the DN):

    public void RemoveUserFromGroup(string userDn, string groupDn)
    {
        try
        {
            DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
            dirEntry.Properties["member"].Remove(userDn);
            dirEntry.CommitChanges();
            dirEntry.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
            //doSomething with E.Message.ToString();
    
        }
    }
    

    Does that work for you??

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to delete a user's schema from a database and I'm getting the
I'm trying to delete a directory that contains XML files from a remote computer.
I am trying to delete an old user from our perforce installation. A previous
I am trying to delete several rows from a MySQL 5.0.45 database: delete from
I'm trying to write a windows batch file that can delete files from subdirectories.
From a user form: I am trying to insert the following data: 1) First
I'm experimenting on this code that I got from the net (I'm trying to
I'm simply trying to delete a row from a DataGridView. Basically, the first column
I know this is a simple syntax issue. Trying to delete all users from
I'm trying to port some code from Linux to Windows. I really don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.