Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create users in Active Directory and Exchange 2010 from CSV file

Posted on 2014-01-15
12
Medium Priority
?
889 Views
Last Modified: 2014-01-16
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?
0
Comment
Question by:jdube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 19

Assisted Solution

by:helpfinder
helpfinder earned 80 total points
ID: 39783888
0
 

Author Comment

by:jdube
ID: 39783910
Is there a cross reference to the field names? E.g. What is the Telephone field, what is the Office field?
0
 
LVL 5

Assisted Solution

by:alicain
alicain earned 80 total points
ID: 39783925
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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 240 total points
ID: 39784067
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
 
LVL 12

Accepted Solution

by:
Vaseem Mohammed earned 1600 total points
ID: 39784762
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
 

Author Comment

by:jdube
ID: 39785337
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
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 39785470
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
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 39785488
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
 

Author Closing Comment

by:jdube
ID: 39785683
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
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 39785688
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
 

Author Comment

by:jdube
ID: 39785893
Thanks Wasim, you are very thorough, it is very much appreciated!
0
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 39786473
You are always welcome.
Your requirements were clear and to the point which helped me to come up with a solution.
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question