Set Organizational unit (OU) after creating windows domain account

1

The following C# code creates a windows domain user account

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN"))
{
  UserPrincipal uc = new UserPrincipal(pc, "username", "password", true);
  uc.Save();
}

How can I specify the OU at the time of account creation? It seems like it should be simple but I am having no luck. I tried using LDAP but gave up after several "Unknown error (0x80005000)" messages. Is there a way to do it using UserPrincipal, GroupPrincipal or some other Principal that I haven't yet found?

c#
.net-4.5
asked on Stack Overflow Dec 26, 2013 by (unknown user)

3 Answers

1

You should do it like that:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "DOMAIN", "OU=ou1" , "OU=ou2" , DC=yourdomain, DC=com, username, password))

This is from my code:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "IPAddress", "OU=NEW, OU=CloudUsers, DC=yourdomain, DC=com", username, password);
answered on Stack Overflow Sep 1, 2015 by Maayan Cahani • edited May 31, 2018 by Kiquenet
0

I'm sorry I cannot test it right now, but I think you should use the PrincipalContext constructor that lets you specify your OU as container: http://msdn.microsoft.com/en-us/library/bb348316(v=vs.110).aspx

answered on Stack Overflow Dec 26, 2013 by Paolo Tedesco
0

To add new OU, fowlling this code:

GroupPrincipal newOU = new GroupPrincipal(pc);
            newOU.Name = "yourOUName";
            newOU.Save();
answered on Stack Overflow Sep 1, 2015 by Maayan Cahani • edited May 31, 2018 by Kiquenet

User contributions licensed under CC BY-SA 3.0