AD User/Exchange Mailbox through Powershell and CSV file

Hello All,

I am trying to write a Powershell script that will take the data from a CSV file and create and AD user and Exchange Mailbox.

My CSV file has the following columns:
UserPrincipalName, Name, Alias, Database, SamAccountName, GivenName, OtherName, Surname, DisplayName,Path,AccountPassword, Organization, Department, Title, Office, City, State, OfficePhone, MobilePhone, Manager (There are a few more fields I will be adding as well).

My script looks like:
Import-Module ActiveDirectory
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

$source = "C:\users.csv"

$csv = Import-Csv -UseCulture -Path $source
$csv | New-Mailbox
$csv | ForEach-Object {
$Identity = $_.SamAccountName
$Password = $_.AccountPassword

Set-ADAccountPassword $Identity -Reset -NewPassword (ConvertTo-SecureString $Password -AsPlainText -Force)
Set-ADUser $Identity -Enabled $true -ChangePasswordAtLogon $true


Open in new window

When I run the script I get the following error after the [$csv | New-Mailbox] command.

New-Mailbox : The parameter "-UserPrincipalName" is required.
At line:1 char:19
+ $csv | New-Mailbox <<<<
    + CategoryInfo          : NotSpecified: (:) [New-Mailbox], RecipientTaskException
    + FullyQualifiedErrorId : F58097DC,Microsoft.Exchange.Management.RecipientTasks.NewMailbox

New-Mailbox : Cannot bind parameter 'Name' to the target. Exception setting "Name": "The property can't be empty."
At line:1 char:19
+ $csv | New-Mailbox <<<<
    + CategoryInfo          : WriteError: (@{UserPrincipal...ager=bashipman}:PSObject) [New-Mailbox], ParameterBinding
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.NewMailbox

Any ideas?

Thanks in advance,


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

I think the issue is that the columns from the CSV are not converted to the parameters directly. You may refer to the article: to solve this issue.

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
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
Microsoft Applications

From novice to tech pro — start learning today.