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?
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.