Share via


Adding Members to a Group

When a group is created, members must be added to the group. This topic describes how to add members to a group.

Members are added to a group by adding the distinguished name of the member to the member attribute of the group. (For more information about the member attribute in the Active Directory Schema, see the topic "Member Attribute" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.) This can be accomplished with either the Add or AddRange method.

To add a single member to a group, add the member's distinguished name to the member property on the group object using the Add method.

Note

The following examples require the .NET Framework version 1.0 Service Pack 3 and later or the .NET Framework version 1.1 Service Pack 1 and later.

The following C# example demonstrates how to add a single member to a group.

// Bind to the group.
DirectoryEntry group = dom.Children.Find(groupDN);

// Add the user to the group.
group.Properties["member"].Add(userDN);

// Commit the changes to the group.
group.CommitChanges();

The following Visual Basic .NET example demonstrates how to add a single member to a group.

' Bind to the group.
Dim group As New DirectoryEntry(groupDN)

' Add the user to the group.
group.Properties("member").Add(userDN)

' Commit the changes to the group.
group.CommitChanges()

To add multiple members to a group in a single operation, add the members' distinguished names to the member property on the group object using the AddRange method.

The following C# example demonstrates how to add multiple members to a group.

// Bind to the group.
DirectoryEntry group = dom.Children.Find(groupDN);

// To add multiple users to a group, use the AddRange method.
group.Properties["member"].AddRange(new string[] {userDN1, userDN2});

// Commit the changes to the group.
group.CommitChanges();

The following Visual Basic .NET example demonstrates how to add multiple members to a group.

' Bind to the group.
Dim group As New DirectoryEntry(groupDN)

' To add multiple users to a group, use the AddRange method.
group.Properties("member").AddRange(New String() {userDN1, userDN2})

' Commit the changes to the group.
group.CommitChanges()

See Also

Reference

PropertyValueCollection
System.DirectoryServices

Concepts

Group Management

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.