Use power shell and excel to create mailboxs

We are migrating from a hosted exchange to a local exchange  ..

Here is my issue, i have an excel spread sheet with 218 hosted user names & email addressess.  I would like to create a powershell script to import from Excel below are the issues:

1) some user allready have AD accounts in 1 of 3 user containers..
2) some dont have an AD account

So it would need to check AD and if there is not an account create in container X if there is than just create mailbox for exisiting user..

anyhelp would be appriciated..
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mat_sullivanInfrastructure EngineerCommented:
I would use 3 separate scripts to do this.

one to identify which names on the list have existing AD accounts

one to ad mailboxes to the existing users

one to creat the new users with mailboxes
mat_sullivanInfrastructure EngineerCommented:
If you have the option to move the mailboxless users to an separate OU the following script will mail enable them and also add multiple amtp addresses based on a naming convention.

It's what I use when I have to mail enable a bunch of AD accounts.

Review it and make sure it does what you want before you try to run it, I take no responsibility for it breaking somthing in your environment.

It requires the Quest AD CMDlets installed to work.
#This Script takes a bunch of users in a specified OU 
#then creates mailboxes and assigns Primary and other additional SMTP addresses


add-PSSnapin quest.activeroles.admanagement #Required for QADUser command

#$OU = "Domain/OU/OU/OU" # where 'Mailboxless' users reside, Change accordingly

$DB = "Server\storagegroup\maildb" #Change as required
get-user –organizationalUnit $OU `
| select Name,firstname,lastname,samaccountname | export-csv -path C:\temp\mailenable.csv #Gets attributes with which to build SMTP Addresses change path accordingly

get-user –organizationalUnit $OU | enable-mailbox -database $DB | set-mailbox -EmailAddressPolicyEnabled $false  # Creates the Mailbox

#This section imports the CSV created previously and uses it to assign new addresses

import-csv "c:\temp\mailenable.csv" | foreach {

$NMB = get-mailbox $_.SamAccountName 

$NMB.emailAddressPolicyEnabled = $false #Enables the change of primary SMTP address
$NMB.primarySmtpAddress = (($_.FirstName+"."+$_.LastName)+"") #Add & Set Primary SMTP 
$NMB.EmailAddresses.Add(($_.FirstName+"."+$_.LastName)+"") #Add additional SMTP address 
$NMB.EmailAddresses.Add(($_.FirstName+"."+$_.LastName)+"") #Add additional SMTP address
$NMB.EmailAddresses.Add(($_.FirstName+"."+$_.LastName)+"@yourotherdomain3") #Add additional Aon SMTP address 
Set-Mailbox -Instance $NMB }

Remove-Item c:\Temp\MailEnable.csv  #Removes the Imported CSV File 
Get-QADUser -SearchRoot $OU | Select Name,SamAccountName,Mail | Export-Csv C:\temp\RecentEmailEnabeldUsers.csv #creates a report detailing what has been created.

Open in new window

You could do domething like this. This is just a start. You can use get-help COMMAND -full to get more examples. or if you have any questions let us know.

This will also depend on how your csv file headers are. Right now I just have the users ogon name as name field.

$users = import-csv c:\users.csv
Foreach($usr in $users){
$uchk = get-user DOMAIN\$
If ($uchk -eq $null){
Elseif($uchk -eq "User"){
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

DUlrich1227Author Commented:
not really an option to move mailboxless users to a seperate OU underlying issue is ...

3 sites

2 sites  are are a mix of users using an alternate hosted solution from MS so out of 600 users maybe 175 of these will be local exchange the 3rd site is our corporate office which is all local exchange and the other 35 .  On the 2 sites all the users are in 1 users container.. ( i think, will have to double check ), my spread sheet is just "display name" and email address..

based on the 3 steps mention above

I guess I need a script to identify who is not in AD based on a display name or do identify who is and in which user container?

at that point i can than pull them to a new container under each site called needs email or something..?

and than a scirpt for each contianer to create the email?  ...
The sample script I posted will check the user in AD.

run the get-user cmdlet
if it returns NULL the users does not exist
If it returns USER no mailbox
if it returns USERMAILBOX the user has a mailbox
mat_sullivanInfrastructure EngineerCommented:
I use a script that targets an OU because I am allways wary about running scripts against AD objects and running against a single OU limits the damage if things go wrong, I would definately recommend running it in a lab environment first to check it works in your setup. But yes, as long as the objects are in one OU then the script will mail enable them and set any SMTP address you specify, obviously you will need to do a bit of editing to tailor it to your server, email address scheme and database names etc...
DUlrich1227Author Commented:
this has been resolved...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DUlrich1227Author Commented:
I was able to work around and not have to use above
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.