Create users in Active Directory and Exchange 2010 from CSV file

I frequently have to create new users who are temporary but is quite tedious because of all of the information needed to put into the AD database. Is there a script I can run to create the users from a CSV file? I would also need to create an email account in Exchange 2010.

I need to enter the fields FirstName, LastName, DisplayName, Description, Office, TelephoneNumber, ProfileLogonScript, JobTitle, Department, and insert users in groups. Is there also a way to set PasswordNeverExpires as well?

Is this possible?
jdubeAsked:
Who is Participating?
 
Vaseem MohammedConnect With a Mentor Commented:
I came up with this script for you.
Note: Active directory module must be available on your "Management Desktop" and "Exchange management console" must be installed.
This script is doing 2 tasks at once.
1. Creating User accounts from the details available in .csv file
2. Enabling the mailbox for the created user.

I am attaching the UserList excel file that I have used, once you have completed the data in excel save it as .csv, after saving cross check that it is a .csv file
The .csv file should be in same directory where you save the script (.ps1), if not then provide a complete path on line:04

Copy and Paste the below script in notepad and [Save As] "ScriptName.ps1" (including quotes)
Write-Host -ForegroundColor Magenta "Importing Module and Snap-in"
Import-Module activedirectory -ErrorAction Stop
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction Stop
sleep -Seconds 10

$UserList = Import-Csv .\userlist-02.csv 
#Same Password will be set for all accounts
$Password = Read-Host "Enter Password for all Accounts " -AsSecureString
$dtm = Get-Date

ForEach($user in $UserList){
Write-Host -ForegroundColor Magenta "CREATING USER"$User.Name
    New-AdUser -Name $User.Name`
    -DisplayName $User.DisplayName`
    -Surname $User.LastName`
    -GivenName $User.FirstName`
    -SamAccountName $User.SAMAccountName`
    -UserPrincipalName $User.UPN`
    -Title $User.JobTitle`
    -OfficePhone $User.OfficePhone -MobilePhone $User.MobilePhone`
    -PasswordNeverExpires $True -Description $User.Description`
    -Department $User.Department`
    -Enabled $True -Path $User.OU`
    -ScriptPath $User.LogonScript -AccountPassword $Password

    Write-Host -ForegroundColor Yellow "  Completing, Please wait..."
    sleep -Seconds 10
    
    Write-Host -ForegroundColor Yellow "  Creating Mailbox for"$User.Name
    Enable-Mailbox -Identity $User.SAMAccountName -Alias $User.SAMAccountName | Out-Null

    Write-Host -ForegroundColor Yellow "  Adding"$User.Name"to"$User.Group
    Add-ADGroupMember -Identity $User.Group -Members $User.SAMAccountName
    Write-Host -ForegroundColor Green "DONE!."
}

get-mailbox -ResultSize Unlimited |
where {$_.WhenMailboxCreated -gt $dtm} |
select Name,
@{n="Display Name";e={$_.DisplayName}},
Alias,
@{n="Account Name";e={$_.SamAccountName}},
@{n="Email";e={$_.PrimarySMTPAddress}},
Database,
#@{n="Mailbox Created On";e={$_.WhenMailboxCreated}},
@{n="User Created On";e={$_.WhenCreated}},
@{n="OU";e={$_.OrganizationalUnit}} | FT -AutoSize

Open in new window

password promptoutputUserList-02.xlsx
0
 
jdubeAuthor Commented:
Is there a cross reference to the field names? E.g. What is the Telephone field, what is the Office field?
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
alicainConnect With a Mentor Commented:
There are several good examples of this on the TechNet script center, for example this one :
http://gallery.technet.microsoft.com/scriptcenter/AD-and-mailbox-from-CSV-96a4713f

The New-ADUser cmdlet has a parmeter that can be used for setting Password Never Expires :
   -PasswordNeverExpires $true


Regards,
Alastair.
0
 
Will SzymkowskiConnect With a Mentor Senior Solution ArchitectCommented:
You need to perform this in 2 steps as they are from 2 different module/snapins...
Create New AD User
Import-Module activedirectory
$Userlist = Import-Csv "c:\youCSVhere.csv"
$Userlist = ForEach ($Person in $Userlist) {
$Person.SamAccountName
$Person.UPN
$Person.Name
$Person.GivenName
$Person.Surname
$Person.Title
$Person.Office
$Person.Description
$Person.Department
$Person.Password
New-ADUser -UserPrincipalName $Person.UPN -SamAccountName $Person.SamAccountName -Name $Person.Name -GivenName $Person.Givenname -Surname $Person.Surname -Title $Person.Title -Department $Person.Department -Office $Person.Office -OfficePhone $Person.OfficePhone -Description $Person.Description -PasswordNeverExpires $True -AccountPassword (ConvertTo-SecureString -AsPlainText $Person.Password)
}

Open in new window


CSV file needs to be constructed like below...
UPN samaccountname Name GivenName SurName Title Office Description Department Password

Once the Users have been created in AD you can use the Enable-mailbox to create a mailbox for them...
$UserList = Import-Csv "C:\youCSVhere.csv"
$UserList = foreach ($Mailbox in $UserList) {
$Mailbox.UserPrincipalName
$Mailbox.Database
Enable-mailbox -Identity $Mailbox.UserPrincipalName -Database $Mailbox.Database
}

Open in new window


CSV File needs to be constructed like below...
UserPrincipalName    Database
sadams@domain.com    servername\database
etc...

Will.
0
 
jdubeAuthor Commented:
Wasim - fantastic answer! One question and one change please:
Why the leading zeros in the phone numbers (just curious)?
Change: How would I add multiple groups?
Thanks!!
0
 
Vaseem MohammedCommented:
You can put whatever phone number you want, all data has to be maintained in excel file.
You can add user to any group, just specify group name in excel sheet, but make sure the group name is correct.
0
 
Vaseem MohammedCommented:
If u mean a user to multiple group? Then i dont think it can be covered in this script, for that more code needs to be added.
Something like taking the list of groups, breaking the list and looping again for each group.
0
 
jdubeAuthor Commented:
I have to give a little something to those who took the time to respond and show they are interested in helping, thank you for your response! The answer I was looking for was correctly answered by Wasim Shaikh. Thank you very, very much - the time you took to put together a concise and detailed solution is greatly appreciated!!
0
 
Vaseem MohammedCommented:
You can put multiple group names in Excel file under Group Column seperating each group by "comma" (,) and use the code below
Write-Host -ForegroundColor Magenta "Importing Module and Snap-in"
Import-Module activedirectory -ErrorAction Stop
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction Stop
sleep -Seconds 10

$UserList = Import-Csv .\userlist-02.csv 
#Same Password will be set for all accounts
$Password = Read-Host "Enter Password for all Accounts " -AsSecureString
$dtm = Get-Date

ForEach($user in $UserList){
Write-Host -ForegroundColor Magenta "CREATING USER"$User.Name
    New-AdUser -Name $User.Name`
    -DisplayName $User.DisplayName`
    -Surname $User.LastName`
    -GivenName $User.FirstName`
    -SamAccountName $User.SAMAccountName`
    -UserPrincipalName $User.UPN`
    -Title $User.JobTitle`
    -OfficePhone $User.OfficePhone -MobilePhone $User.MobilePhone`
    -PasswordNeverExpires $True -Description $User.Description`
    -Department $User.Department`
    -Enabled $True -Path $User.OU`
    -ScriptPath $User.LogonScript -AccountPassword $Password

    Write-Host -ForegroundColor Yellow "  Completing, Please wait..."
    sleep -Seconds 10
    
    Write-Host -ForegroundColor Yellow "  Creating Mailbox for"$User.Name
    Enable-Mailbox -Identity $User.SAMAccountName -Alias $User.SAMAccountName | Out-Null

    $Groups = $User.Group
    $GroupList = $Groups -split(",")
    foreach($Group in $GroupList){
    Write-Host -ForegroundColor Yellow "  Adding"$User.Name"to"$Group
    Add-ADGroupMember -Identity $User.Group -Members $User.SAMAccountName
}
    Write-Host -ForegroundColor Green "DONE!."
}

get-mailbox -ResultSize Unlimited |
where {$_.WhenMailboxCreated -gt $dtm} |
select Name,
@{n="Display Name";e={$_.DisplayName}},
Alias,
@{n="Account Name";e={$_.SamAccountName}},
@{n="Email";e={$_.PrimarySMTPAddress}},
Database,
#@{n="Mailbox Created On";e={$_.WhenMailboxCreated}},
@{n="User Created On";e={$_.WhenCreated}},
@{n="OU";e={$_.OrganizationalUnit}} | FT -AutoSize

Open in new window

0
 
jdubeAuthor Commented:
Thanks Wasim, you are very thorough, it is very much appreciated!
0
 
Vaseem MohammedCommented:
You are always welcome.
Your requirements were clear and to the point which helped me to come up with a solution.
0
All Courses

From novice to tech pro — start learning today.