Powershell – Using PowerShell to create user accounts from a CSV

active-directorypowershell

How can I create a domain user account and set the default password in PowerShell from a CSV?

Note that I know how to do this, I'm writing this as a self-answered question to share this knowledge. If anyone has a better answer, please chime in!

Best Answer

This answer assumes that you have a CSV with two columns that have the headings: username and password.

It doesn't take make other attributes, such as firstname, lastname, manager, department, etc. They can all be added and are outlined in the get-help new-aduser -full command.

This is the bare minimum that needs to be done to automate account creation. In production, you'll probably want to force password change on first logon as well as setting many other attributes.

Import-Module ActiveDirectory

Import-CSV c:\users\marra\desktop\userlist.csv | ForEach-Object {
    $name = $_.username                                               
    $pw = $_.password 
    $ss = ConvertTo-SecureString -string $pw -asPlainText -force        

    New-ADUser $name -SamAccountName $name -AccountPassword $ss -Path "OU=MyUserOU,DC=mydomain,dc=com" -Enabled $TRUE        
    }