title: SQL Query for Disabled Active Directory Accounts
body: <p>I need to query AD to determine if a users account is disabled.</p>
  
  <p>Using a similar query used in the answers <a href="https://stackoverflow.com/questions/1251038/querying-active-directory-from-sql-server-2005">here</a> </p>
  
  <pre><code>SELECT *
  FROM OPENQUERY(ADSI, 'SELECT sAMAccountName
  FROM ''LDAP://DC=MyDC,DC=com,DC=uk''
  WHERE objectCategory = ''Person''
  AND objectClass = ''user'')
  </code></pre>
  
  <p>I believe to determine if an account is disabled I have to use the userAccountControl field somehow. I've tried several things but they don't seem to be working:</p>
  
  <pre><code>WHERE userAccountControl &amp; 2 &lt;&gt; 0
  </code></pre>