title: Active Directory Creating An User
body: <p>I'm trying to add an user to the active directory via a C# script. I've found this script on the internet (I didn't made it myself). The problem is that I get this error when I'm trying to add an user:</p>
  
  <blockquote>
    <p>The specified directory service attribute or value does not exist.</p>
  </blockquote>
  
  <p>This is the code I have right now:</p>
  
  <pre><code>private void buttonCreateUser_Click(object sender, EventArgs e)
  {
      CreateADSUser(textboxUsername.Text, textboxPassword.Text);
  }
  
  public string CreateADSUser(string username, string password)
  {
      String RootDSE;
      try
      {
          DirectorySearcher DSESearcher = new DirectorySearcher();
          RootDSE = DSESearcher.SearchRoot.Path;
  
          RootDSE = RootDSE.Insert(7, "CN=Users,");
  
          DirectoryEntry myDE = new DirectoryEntry(RootDSE);
          DirectoryEntries myEntries = myDE.Children;
  
          DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user");
          myDirectoryEntry.Properties["userPrincipalName"].Value = username;
          myDirectoryEntry.Properties["name"].Value = username;
          myDirectoryEntry.Properties["Password"].Value = password;
          myDirectoryEntry.Properties["samAccountName"].Value = username;
          myDirectoryEntry.Properties["FullName"].Value = username;
          myDirectoryEntry.Properties["AccountDisabled"].Value = 0;
          myDirectoryEntry.Properties["PasswordRequired"].Value = 1;
  
          // Permanent Password?
          myDirectoryEntry.Properties["permpass"].Value = 1;
          myDirectoryEntry.CommitChanges();
  
          DSESearcher.Dispose();
          myDirectoryEntry.Dispose();
  
          textboxReports.Text = "Worked!";
          return "Worked!";
      }
      catch (Exception ex)
      {
          textboxReports.Text = ex.Message;
          return ex.Message;
      }
  }
  </code></pre>