Majkl
Majkl

Reputation: 187

Create new mailboxes using a CSV via Powershell in Office 365

I need to create new mailboxes via Powershell in Office 365. I am using this script:

$User = "[email protected]" 
$PWord = ConvertTo-SecureString -AsPlainText -Force -String "P@ssword1"
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection 
Import-PSSession $Session

Import-CSV new.csv | foreach { 
    New-Mailbox -UserPrincipalName $_.UserPrincipalName -displayname $_.DisplayName -password (ConvertTo-SecureString $_.password -AsPlainText -Force) -usagelocation "us" 
}

Get-PSSession | Remove-PSSession

Details of mailboxes are saved in new.csv file. See the following example:

UserPrincipalName,DisplayName,password
[email protected],Clark Kent,P@ssword1
[email protected],Bruce Wayne,P@ssword1
[email protected],Peter Parker,P@ssword1

When I run this script, I return error:

A parameter cannot be found that matches parameter name 'UserPrincipalName'.
    + CategoryInfo          : InvalidArgument: (:) [New-Mailbox], 

ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,New-Mailbox
    + PSComputerName        : outlook.office365.com

Please, what is wrong? Can you help me?

Upvotes: 1

Views: 3540

Answers (3)

Nick Eagle
Nick Eagle

Reputation: 1208

The UPN Parameter is only available on the Exchange on-premises. Depending on on whether your AD is on-premises or in the cloud, I would suggest creating the AD account first with this parameter and then enable the mailbox.

Or just don't use this parameter.

Upvotes: 2

MadBoy
MadBoy

Reputation: 11104

Correct way to do this is by doing this via MicrosoftOnlineServicesID parameter which seems to replicate UPN.

New-Mailbox -Alias $mailbox.Alias -Name $mailbox.Name -DisplayName $mailbox.DisplayName -ResetPasswordOnNextLogon $true -Password $temporaryPassword -MicrosoftOnlineServicesID $upn -WhatIf

WARNING: After you create a new mailbox, you must go to the Office 365 Admin Center and assign the mailbox a license, or it will
 be disabled after the grace period.
What if: Creating mailbox "Anna" with User Principal Name "[email protected]" in organizational
 unit "EURPR06A004.PROD.OUTLOOK.COM/Microsoft Exchange Hosted Organizations/domain.onmicrosoft.com".

Full story can be found at my blog but all you need is to change UPN to MicrosoftOnlineServicesID and it should work right away.

Upvotes: 1

Office 365
Office 365

Reputation: 1

Try -identity instead of -userprincipalname

Upvotes: 0

Related Questions