Create Shared Mailboxes with CSV and PowerShell


For the purpose of creating Shared Mailboxes in Office 365/Exchange Online I created a PowerShell script which uses a CSV file for input. The file must be saved as “Unicode, Semicolon separated” (;) and must contain the following fields:

Name Description
Name Name of the Shared Mailbox (this field cannot contain spaces!)
Display Name Full / Display Name
Email Primary email address (this field can contain only one address)
Alias Email alias address(es) – when using more than one: separate them by using a comma (,)
Members Specify the primary email addresses for users who will have access to this mailbox – separate them by using a comma (,)

During the process of creating Shared Mailbox(es) the script creates a security enabled Distribution Group with the name SG_”SharedMailbox Name”. This Distribution Group is given full access to the mailbox. So when a user needs access to the group they only have to be a member of the Distribution Group. Make sure that you give the right people (normally the IT department) owner rights for this Distribution Group. Because only owners can add members to the group. You need to specify the primary email addresses for the owners in the PowerShell script! Separate the email addresses by using a comma (,)

Script

Here’s the code for the script:

#### Set Variable
Set-Variable -name Domain -value wortelltechready.com
Set-Variable -name DGOwners -value admin@wortelltechready.com,user1@wortelltechready.com
#### Create Function Logon to Office365 - Exchange Online
function Logon {
 #### Pop-up a dialog for username and request your password
 $cred = Get-Credential
 #### Import the Local Microsoft Online PowerShell Module Cmdlets and Connect to O365 Online
 Import-Module MSOnline
 Connect-MsolService -Credential $cred
 #### Establish an Remote PowerShell Session to Exchange Online
 $msoExchangeURL = “https://ps.outlook.com/powershell/”
 $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $msoExchangeURL -Credential $cred -Authentication Basic -AllowRedirection
 Import-PSSession $session
 }

#### Create Function Logoff Office365 & Exchange Online
function Logoff {
 #### Remove the Remote PowerShell Session to Exchange Online ----
 Get-PsSession | Remove-PsSession
 #Remove-PsSession $session
 }

############################################################################################################################
############################################################################################################################

#### Logon to Office 365 & Exchange Online
Logon
#### Ask the user for input CSV File
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ofd = New-Object System.Windows.Forms.OpenFileDialog
#$ofd.InitialDirectory = "d:\scripts"
$ofd.ShowHelp=$true
if($ofd.ShowDialog() -eq "OK") { $ofd.FileName }
$File = $ofd.Filename

#### Create Log File + Start Logging
if ($File -ne $Null) {
$Log = $File + ".log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path $Log -append
 }

#### Import CSV
Import-csv -Delimiter ";" $File | ForEach {

#### Create Mailbox
New-Mailbox -Name $_."Name" -DisplayName $_."Display Name" -PrimarySmtpAddress $_."Email" -Shared

#### Set Mailbox Size for Shared Mailboxes
Set-Mailbox $_."Name" -ProhibitSendReceiveQuota 5GB -ProhibitSendQuota 4.75GB -IssueWarningQuota 4.5GB -MicrosoftOnlineServicesID $_."Email"

#### Add Aliases
$Aliases = $_.Alias -split ','
if ($Aliases -ne $Null) {
 Set-Mailbox $_."Name" -EmailAddresses @{add= $Aliases}
 }

#### Create Security Group with name SG_"Distribution Group Name", add Members and set Owners
$Name=$_."Name"
$DGName="SG_" + $Name
$DGMail=$DGName + "@" + $Domain
New-DistributionGroup -Name $DGName -Type Security -PrimarySmtpAddress $DGMail
$Member = $_.Members -split ","
$Member | Foreach-object {
Add-DistributionGroupMember -Identity $DGName -Member $_
 }
Set-DistributionGroup $DGName -HiddenFromAddressListsEnabled $true
Set-DistributionGroup $DGName -BypassSecurityGroupManagerCheck -ManagedBy $DGOwners
#### Add permissions for Distribution Group
Add-MailboxPermission $Name -User $DGName -AccessRights FullAccess
Add-RecipientPermission $Name -Trustee $DGName -AccessRights SendAs -Confirm:$False

 

}
#### Stop Logging
Stop-Transcript
#### Logoff
Logoff

Copy and paste the code in notepad (for example) and save it as “CreateSharedMailbox.ps1”. Go through the following steps to use the script.

Steps

This is part 2 of a series of posts about some PowerShell scripts I created or used and modified for some Office 365/Exchange Online migrations.

In part 3 of this series I will share a script which can be used to convert Shared Mailbox(es) to Regular Mailbox(es).

An overview of the series can be found here.

Download a sample CVS file from here: CreateSharedMailbox.csv. After the download, rename the file to “CreateSharedMailbox.csv”.

2 thoughts on “Create Shared Mailboxes with CSV and PowerShell

Add yours

  1. Firstly, I’d like to thank you for posting this its exactly what I need!

    I’m having some trouble running this command, I created a a csv file with the columns Name, Display name, Email address and Members. I think my problem may be in the formatting of this file however a second opinion would be great.

    This is what the log file is returning:

    Cannot bind argument to parameter ‘Name’ because it is an empty string.
    + CategoryInfo : InvalidData: (:) [New-Mailbox], ParameterBinding
    ValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
    lowed,New-Mailbox

    Cannot process argument transformation on parameter ‘MicrosoftOnlineServicesID’
    . Cannot convert null to type “Microsoft.Exchange.Data.SmtpAddress”.
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin.
    ..mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox

    Active Directory operation failed on DBXPR05DC002.eurprd05.prod.outlook.com. Th
    e object ‘CN=SG_,OU=oguk.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizat
    ions,DC=eurprd05,DC=prod,DC=outlook,DC=com’ already exists.
    + CategoryInfo : NotSpecified: (:) [New-DistributionGroup], ADObj
    ectAlreadyExistsException
    + FullyQualifiedErrorId : [Server=DBXPR05MB112,RequestId=f0909a43-02ac-46a
    2-b5d5-20ef334da784,TimeStamp=29/10/2013 15:33:52] [FailureCategory=Cmdlet
    -ADObjectAlreadyExistsException] E9CD4F98,Microsoft.Exchange.Management.Re
    cipientTasks.NewDistributionGroup

    Cannot process argument transformation on parameter ‘Member’. Cannot convert va
    lue “” to type “Microsoft.Exchange.Configuration.Tasks.RecipientWithAdUserGroup
    IdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientIdParameter]”. Er
    ror: “Parameter values of type Microsoft.Exchange.Configuration.Tasks.Recipient
    WithAdUserGroupIdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientId
    Parameter] can’t be empty. Specify a value, and try again.
    Parameter name: identity”
    + CategoryInfo : InvalidData: (:) [Add-DistributionGroupMember],
    ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-Distrib
    utionGroupMember

    WARNING: The command completed successfully but no settings of ‘SG_’ have been
    modified.
    Cannot bind argument to parameter ‘Identity’ because it is null.
    + CategoryInfo : InvalidData: (:) [Add-MailboxPermission], Parame
    terBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,A
    dd-MailboxPermission

    Cannot bind argument to parameter ‘Identity’ because it is null.
    + CategoryInfo : InvalidData: (:) [Add-RecipientPermission], Para
    meterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,A
    dd-RecipientPermission

    The term ‘nbsp’ is not recognized as the name of a cmdlet, function, script fil
    e, or operable program. Check the spelling of the name, or if a path was includ
    ed, verify that the path is correct and try again.
    At C:\Users\phil.rennie\Documents\New folder\shared mailbox.ps1:76 char:2
    + & <<<< nbsp;
    + CategoryInfo : ObjectNotFound: (nbsp:String) [], CommandNotFoun
    dException
    + FullyQualifiedErrorId : CommandNotFoundException

    Cannot bind argument to parameter 'Name' because it is an empty string.
    + CategoryInfo : InvalidData: (:) [New-Mailbox], ParameterBinding
    ValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
    lowed,New-Mailbox

    Cannot process argument transformation on parameter 'MicrosoftOnlineServicesID'
    . Cannot convert null to type "Microsoft.Exchange.Data.SmtpAddress".
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin.
    ..mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox

    Active Directory operation failed on DBXPR05DC002.eurprd05.prod.outlook.com. Th
    e object 'CN=SG_,OU=oguk.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizat
    ions,DC=eurprd05,DC=prod,DC=outlook,DC=com' already exists.
    + CategoryInfo : NotSpecified: (:) [New-DistributionGroup], ADObj
    ectAlreadyExistsException
    + FullyQualifiedErrorId : [Server=DBXPR05MB112,RequestId=36040fe2-6ca7-480
    9-a75b-7c9093593cf2,TimeStamp=29/10/2013 15:33:54] [FailureCategory=Cmdlet
    -ADObjectAlreadyExistsException] E9CD4F98,Microsoft.Exchange.Management.Re
    cipientTasks.NewDistributionGroup

    Cannot process argument transformation on parameter 'Member'. Cannot convert va
    lue "" to type "Microsoft.Exchange.Configuration.Tasks.RecipientWithAdUserGroup
    IdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientIdParameter]". Er
    ror: "Parameter values of type Microsoft.Exchange.Configuration.Tasks.Recipient
    WithAdUserGroupIdParameter`1[Microsoft.Exchange.Configuration.Tasks.RecipientId
    Parameter] can't be empty. Specify a value, and try again.
    Parameter name: identity"
    + CategoryInfo : InvalidData: (:) [Add-DistributionGroupMember],
    ParameterBindin…mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-Distrib
    utionGroupMember

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: