Working with Active Directory and PowerShell (part 2) – Creating a user

To create a user in our Organizational Unit we need the following LDAP string:
$LDAPStringUsers = “LDAP://OU=Demo Users, DC=contoso,DC=com”

And run the following command to connect to AD:
$usersOU = [ADSI] $LDAPStringUsers

To create a user run this command:
$newUser = $usersOU.Create(“user”,”cn=Kalle Becker “)
$newUser.Put(“sAMAccountName”, “Kalle”)

We need a password for this user, and I actually found a command that will do this:

With that done let’s enable the account (the account is disabled from the beginning)
$newUser.psbase.InvokeSet(‘AccountDisabled’, $false)

So around here is where it gets tricky. I want to set a lot of properties for my user (not only password). I went to and search for “PowerShell set first name ad user” and found out that the command looks like this:
Ok – I could have guessed that one. But how about setting the login name for the user?
Login, LoginName? Nope, it’s UserPrincipalName. Couldn’t have guessed that!

I found a page (sorry but I can’t seem to find my way back to it, but it’s on the net J) that taught me how to look up what the first parameter in InvokeSet should be. Here’s how you do it:

  1. Get your hands on adsiedit, check this link:

  2. Install it (duh!)

  3. Run it, and connect to you AD

  4. Find you user and right-click properties.

This will give you a looong list of stuff that you can set for this user.
To find out which one to user, go into AD (Users and Computers), enter a value in one of the fields (like “aaaStockholm”), go back to ADSIEdit and try to find it in the list. Then you know the name of the Attribute! Take that name and use it with the InvokeSet command in PowerShell!

Some of my users have a manager so here’s a little treat on how to set that in PowerShell:
$teststring = “CN=”+$user.Organization.ManagerName+”,OU=Demo Users,DC=contoso,DC=com”

$teststring is the LDAP string for the manager.

Comments (2)

  1. blablamedias says:

    Hi BomBom,

    exactly what i look for. Is there a possibility to clear or reset the value for 'manager' with InvokeSet?

    Thank you