enumerating active directory accounts

I noticed the UserAccountControl field while I was playing with PowerShell and Active Directory. The field contains a lot of information that could be useful if it was more readable.

I decided to write a script to enumerate the field:

function enum-user ($user = $(throw 'Username Required.')) {
    $userAccountControl = @{
            SCRIPT                          = 0x1;
            ACCOUNTDISABLE                  = 0x2;
            HOMEDIR_REQUIRED                = 0x8;
            LOCKOUT                         = 0x10;
            PASSWD_NOTREQD                  = 0x20;
            PASSWD_CANT_CHANGE              = 0x40;
            ENCRYPTED_TEXT_PWD_ALLOWED      = 0x80;
            TEMP_DUPLICATE_ACCOUNT          = 0x100;
            NORMAL_ACCOUNT                  = 0x200;
            INTERDOMAIN_TRUST_ACCOUNT       = 0x800;
            WORKSTATION_TRUST_ACCOUNT       = 0x1000;
            SERVER_TRUST_ACCOUNT            = 0x2000
            DONT_EXPIRE_ACCOUNT             = 0x10000;
            MNS_LOGON_ACCOUNT               = 0x20000;
            SMARTCARD_REQUIRED              = 0x40000;
            TRUSTED_FOR_DELEGATION          = 0x80000;
            NOT_DELEGATED                   = 0x100000;
            USE_DES_KEY_ONLY                = 0x200000;
            DONT_REQ_PREAUTH                = 0x400000;
            PASSWORD_EXPIRED                = 0x800000;
            TRUSTED_TO_AUTH_FOR_DELEGATION  = 0x1000000;
    }

    $entry = new directoryservices.directoryentry("LDAP://dc=domain,dc=com")
    $search = new directoryservices.directorysearcher($entry)
    $search.filter = "(samaccountname=$user)"

    $user = @($search.findAll())[0]

    write-output `n

    if ($user) {
            $x = [int][string]$user.properties.useraccountcontrol
            $name = [string]$user.properties.givenname + " " + [string]$user.properties.sn

            write-output "$name has the following properties: `n"

            foreach ($control in $userAccountControl.keys) {
                    if ($userAccountControl[$control] -band $x) {
                            write-output $control
                    }
            }
    } else {
            write-output "User Not Found"
    }

    write-output `n
}

The script takes a username (samAccountName) as a parameter and, if the account is found, will print out the readable UserAccountControl flags.