Office 365 – Script to Activate New Mailbox

Script can be downloaded by clicking this Link. Rename the file to PS1 before it can be used.

Please note that once the mailbox has been created, it needs to be activated.

Details of creating new mailboxes in O365 can be found at this link

This script is used to perform the following tasks to a mailbox that has newly been created in Office 365:

  • Set usage location on O365 mailbox to Canada
  • Assigns licenses for Exchange Online, Lync Online, Office365 ProPlus and SharePoint Online
  • Enables retention policy on mailboxes
  • Please note that this script will require an input file.
  • The content of the file should be the SAMAccountName or the UPN of the newly created users in Active Directory.
  • As an example, all new users being created have a logon name of firstname.lastname – this is referred to as the SAMAccountName. The file should contain entries like – each name on its own like:

Clark.Kent
Bruce.Wayne
Bruce.Banner

    • The file can also contain the UPNs of the users in the format:

Bruce.Wayne@domain.ca
Bruce.Banner@domain.ca
Clark.Kent@domain.ca

 

############################################################################################################
# This script will activate the user by:
#    •    setting usage location on O365 mailbox to Canada
#    •    Assigns licenses for Exchange Online, Lync Online, Office365 ProPlus and SharePoint Online
#    •    Enables retention policy on mailboxes
############################################################################################################

######
# AUTHOR: IBRAHIM U. BENNA
######

#This function is used to create a “pause” in the script. Continuation of the script requires an input from the keyboard

Function Pause ($Message=”Press Any Key to Continue…”)
{
Write-host $Message
$Null = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
Write-host ” ”
}

Write-host -foregroundcolor Red “Please make sure you are running this script from the Windows Azure Active Directory PowerShell”

Echo ” ”

Pause

#Import Users to be activated from text file. If no text file input is provided, the script immediately terminates. The file must contain the logon names or the UPNs of the users being activated

If ($Args.count -eq 0) {
Write-host -foregroundcolor Red “You need to specify a file name in the command. The file should contain a list of users to be activate.”
echo ” ”
Write-host -foregroundcolor Red “Please re-run the script providing the file name using the syntax ‘New-O365UserSettings.PS1 InputFileName.txt’ ”
echo ” ”
Exit
}

# Obtain credentials for global administrator in O365
$O365Credentials = Get-Credential

#Create a remote session and connect to Office 365 using Windows PowerShell
$Session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri https://ps.outlook.com/powershell/ -Credential $O365Credentials -Authentication Basic –AllowRedirection
Import-PSSession $Session
connect-msolservice -credential $O365credentials

Echo ” ”

Write-Host -foregroundcolor Green “Importing file with user accounts to be activated”

Echo ” ”
#This section will read the content of the text file
$Users = get-content $Args[0]
#This section assigns the license type
$ServicePlans = Get-MsolAccountSku | fl | Where {$_.SkuPartNumber -eq “ENTERPRISEPACK”}
$ServicePlans.ServiceStatus
$MyO365Sku = New-MsolLicenseOptions -AccountSkuId syndication-account:ENTERPRISEPACK -DisabledPlans RMS_S_ENTERPRISE,SHAREPOINTWAC

#Assign Location attribute, necessary licenses, activate user and also apply the retention policy to the user’s mailbox
Foreach ($U in $Users) {
#Sets Usage Location attribute for mailbox to Canada
echo “enabling Usage Location ”
Set-MsolUser -UserPrincipalName $U -UsageLocation CA

#Sets license to necessary options defined
echo “enabling license options ”
Set-MsolUserLicense -UserPrincipalName $U -addlicenses “syndication-account:ENTERPRISEPACK”
Set-MsolUserLicense -UserPrincipalName $U -licenseoptions $MyO365Sku
echo “$U has now been licensed in O365”

#Applies retention policy to mailbox
Start-ManagedFolderAssistant $U
echo “Archive/Retention Policy successfully applied to $U”

echo ” ”
echo ” ”
}

#Disconnects remote powershell session from O365
get-pssession | remove-pssession