I'm trying to programmatically delete a local user group. I'm using the following code which I've cobbled together from various examples online. From everything I've read, it should work. But the Delete line throws an exception.
PrincipalContext context = new PrincipalContext( ContextType.Machine );
GroupPrincipal group = new GroupPrincipal( context );
PrincipalSearcher ps = new PrincipalSearcher( group );
PrincipalSearchResult<Principal> results = ps.FindAll();
Principal foundGroup = results.SingleOrDefault( item => item.Name == groupName );
if(foundGroup != null)
foundGroup.Delete(); // Here occurs the InvalidOperationException
This throws an InvalidOperationException
with the message:
The Active Directory object located at the path WinNT://LOREM/IPSUM is not a container."
I'm a complete newbie when it comes to this Active Directory stuff.
Can you try
PrincipalSearchResult<GroupPrincipal>
?I am also not sure if your .SingleOrDefault is used correctly. Can you try: