Skip to content

Instantly share code, notes, and snippets.

@SamKr
Last active January 6, 2020 14:02
Show Gist options
  • Save SamKr/43fc08ba124042e6d51df54acbea3fbe to your computer and use it in GitHub Desktop.
Save SamKr/43fc08ba124042e6d51df54acbea3fbe to your computer and use it in GitHub Desktop.
Search for the existance of a local user
using System.DirectoryServices.AccountManagement;
using (var pc = new PrincipalContext(ContextType.Machine))
{
var up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, "gebruiker2");
var userExists = (up != null);
if (userExists) Console.WriteLine("user exists");
else Console.WriteLine("user not found");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment