Q. How can I bulk create accounts in AD from a file?
A. Often you may want to perform bulk actions in PowerShell, creating accounts is just one example. An easy was is if you have a comma-separated value (CSV) file with the objects and attributes you wish to create. For example below is a CSV file for new accounts I want to create. An easy way to create this type of file is an export from a system or edit in something like Excel.
Name,SamAccountName,GivenName,Surname,DisplayName,Description Steven Rogers,Steve,Steven,Rogers,Steven Rogers,Captain America Anthony Stark,Tony,Anthony,Stark,Anthony Stark,Iron Man Henry Pym,Hank,Henry,Pym,Henry Pym,Ant-Man Janet van Dync,Janet,Janet,van Dyne,Jane van Dyne,Wasp Bruce Banner,Bruce,Bruce,Banner,Bruce Banner,Hulk
Note that the first line is the name of the attributes then each line in the file a separate instance.
The Import-CSV cmdlet will import data from a CSV than pass that data out to the pipeline which can then create the objects. In my example below I have the creation using the -passthru switch which sends the created AD object to the next line which adds the user to an AD group.
Import-CSV -Path $env:HOMEPATH\Documents\roster.csv | New-ADUser -Enabled $true -AccountPassword $password ` -Path "OU=Avengers,DC=savilltech,DC=net" -PassThru | Add-ADPrincipalGroupMembership -MemberOf CurrentRoster