<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Bind to Active Directory and create a new user</Title>
      <Shortcut>BindtoActiveDirectoryandcreateanewuser</Shortcut>
      <Description>Bind to Active Directory and create a new user [VB.NET]</Description>
      <Author>J.Marc Piulachs</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=21a6ade1-aec3-400f-bdc8-df4a09a22f33</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[Imports System.DirectoryServices
Imports ActiveDs
Module Module1
    Sub Main()
        ' connect to my Active Directory
        Dim root As New DirectoryEntry("LDAP://MyDomainControllerServer/dc=digeratisoftware,dc=local")
        Try
            ' create a new user object whose RDN is John Brennan
            Dim user As DirectoryEntry = root.Children.Add("CN=John Brennan", "user")
            ' set properties on the user
            user.Properties("givenName").Value = "John"
            user.Properties("sn").Value = "Brennan"
            user.Properties("mail").Value = "john@somemailaddress.com"
            user.Properties("description").Value = "new test user"
            user.Properties("sAMAccountName").Value = "John.Brennan"            
            ' userPrincipalName. This property is domain specific
            user.Properties("description").Value = "John.Brennan@digeratsoftware.local"             
            ' enable the user account and set their password to never expire
            user.Properties("userAccountControl").Value = ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT Or ADS_USER_FLAG.ADS_UF_PASSWD_NOTREQD Or ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD
            ' commit the object from memory to the directory store
            user.CommitChanges()
            ' next set the user's password
            user.Invoke("SetPassword", New Object() {"mypassword"})
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Module]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>