Connect to OU and Create User

I need a script that will determine the domain, connect to an ou (strOU) then Create a user. Please post your script and help here
Who is Participating?
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

Nope, you would need to provide the full path to the OU - this is unavoidable if you want the nice quick way to create the user. If you're to search for the OU then you really only have two options: ADODB (an SQL type query using ADO) or Recursion.

Anyway, I'll describe both of these in your other thread so hopefully it will become clear from that.

Chris DentPowerShell DeveloperCommented:

Lots of questions ;)

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objOU = GetObject("LDAP://OU=SomeOU," & objRootDSE.Get("defaultNamingContext"))

objOU.Create("user", "CN=User Name")

After that you can set user properties as you see fit followed by "objUser.SetInfo".

dion_p1Author Commented:
Will this work if the OU is three or four OU's deep?
Also How do i set the strDomain String for userPrincipalName if im trying to Determine the Domain.

strOU = "ounamehere"
strUserID = "usernamehere"

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objOU = GetObject("LDAP://" & strOU & ," & objRootDSE.Get("defaultNamingContext"))

Set objUser = objOU.Create("user", "CN=" & strUserID)
    objUser.Put "samAccountName", strUserID
    objUser.Put "userPrincipalName", strUserID & strDomain
    objUser.Put "sn", strSurname
    objUser.Put "givenName", strFirstName
    objUser.Put "displayName", strFirstname & " " & strSurname
    objuser.Put "description", "Student User - " & strGroup
All Courses

From novice to tech pro — start learning today.