Question

I'm trying to rename a group. My code looks like the following

PrincipalContext context = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "GroupName");
group.SamAccountName = "NewGroupName";
group.DisplayName = "NewGroupName";
group.Name = "NewGroupName";
group.Save();

The problem is that it crashes at group.DisplayName = "NewGroupName"; and throws the exception

property is not valid for this store type.

If I comment out that line the code will run on but I would like to change the Display name. I did some research and found this. However I am using Framework 4.0. Does anyone know another what to do this or am I going about this the wrong way?

Was it helpful?

Solution

After much deliberation I has found the only way to get group.Name = "Newname"; to work is to get the DirectoryEntry object and then change it. Code below

PrincipalContext context = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "GroupName");
var Groupentry = (DirectoryEntry)group.GetUnderlyingObject();
Groupentry.Rename("newname");
Groupentry.CommitChanges();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top