Enumerate windows users' groups


Written on Tuesday, April 08, 2008 by
The code below will enumerate which groups a domain / local user belongs to. [sourcecode language="csharp"] private static string GetUserGroups(string strUser) { string groups = ""; DirectoryEntry de = null; try { string entryName = String.Format("WinNT://{0},user", strUser.Replace("\\", "/")); de = new DirectoryEntry(entryName); object oGroups = de.Invoke("Groups"); foreach (object o in (IEnumerable)oGroups) { DirectoryEntry group = new DirectoryEntry(oGroups); groups += group.Name + ","; } } catch (Exception) { throw; } return groups; } [/sourcecode]
Back

Let's talk about this

 


The opinions expressed herein are my own and do not represent my employer's view in any way.


Creative Commons License