Checking if a user account is enabled in .NET

Here's a handy little code snippet to figure out if a local windows user account is enabled or not using the System.DirectoryServices namespace.

private static bool IsUserAccountEnabled(string username)
        var result = new DirectoryEntry { Path = "WinNT://" + Environment.MachineName + ",computer" }
            .Where(d => d.SchemaClassName == "User")
            .First(d => d.Properties["Name"].Value.ToString() == username);

        return ((int)result.Properties["UserFlags"].Value & 2) != 2;
        return false;

~Eoin Campbell

Eoin Campbell

Eoin Campbell
Dad, Husband, Coder, Architect, Nerd, Runner, Photographer, Gamer. I work primarily on the Microsoft .NET & Azure Stack for ChannelSight

CPU Spikes in Azure App Services

Working with Azure App Services and plans which have different CPU utilization profiles Continue reading


Published on November 05, 2020

Data Partitioning Strategy in Cosmos DB

Published on June 05, 2018