Email migrations can take weeks to complete but I try to save as much time as I can by making runbooks with Powershell scripts for both my source and destination environment. In this article, I’ve compiled a list of the my favorite and most used scripts during a migration. Feel free to comment with any that you like to use as well.

Connecting to 365 Exchange Online Account

$credential = Get-Credential

Import-Module MsOnline
Connect-MsolService -Credential $credential

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $credential -Authentication “Basic” -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking

Bulk Importing Users with Passwords to 365

Create a CSV with the Following Headers:

  • UserPrincipalName
  • FirstName
  • LastName
  • DisplayName
  • Password

Import-Csv -Path ‘FilePath.csv’ | foreach {New-MsolUser -UserPrincipalName $_.UserPrincipalName -FirstName $_.FirstName -LastName $_.LastName -DisplayName $_.DisplayName -Password $_.Password -ForceChangePassword $False}

Bulk Updating UPN in 365

$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
$testpath = test-path c:\temp; If ($testpath -eq $false) {new-item -type directory c:\temp}; $dataout = @();
Get-MsolUser -All | ? {$_.UserPrincipalName -match “domain.onmicrosoft.com” -and $_.UserPrincipalName -notmatch “admin”} | % {Set-MsolUserPrincipalName -ObjectId $_.objectId -NewUserPrincipalName ($_.UserPrincipalName.Split(“@”)[0] + “@domain.com”); $dataout += “$($_.UserPrincipalName)” ; $_.UserPrincipalName };$dataout | out-file c:\temp\UPNChangeOutput.txt

Adding an Alias’ to all Users 365

$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange 砲onnecti
dential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
$users = Get-Mailbox
foreach ($a in $users) {$a.emailaddresses.Add(“$($a.alias)@domain.com”)
$users | %{Set-Mailbox $_.Identity -EmailAddresses $_.EmailAddresses}

Bulk Upload Distribution Lists with Members

Create Two CSVs :

  • One has the Distribution Group Name and Primary SMTP Address
  • One has the Group name and the members apart of that group

Import-Csv -Path ‘FilePath.csv’ | foreach {New-Distributiongroup -Name $_.Name -PrimarySmtpAddress $_.Address }

Import-Csv ‘FilePath.csv’ | foreach {Add-DistributionGroupMember
-Identity $_.DL -Member $_.Alias}

Bulk Upload Shared Mailboxes

Create a CSV with the Headers of:

  • DisplayName
  • Userprincipalname

Import-Csv -Path ‘FilePath.csv’  | foreach-object { New-Mailbox -Name $_.DisplayName -primarysmtpaddress $_.Userprincipalname -Shared }

Bulk Upload Mailbox Permissions

Create a CSV with the headers of :

  • Identity (This is the person you are giving the permissions to. Format: user@domain.com)
  • User (This is the person whose mailbox is going to be able to be viewed by “Identity” user)

Import-Csv -Path ‘FilePath.csv’ | foreach {Add-MailboxPermission -Identity $_.Identity -User $_.User -AccessRights FullAccess -InheritanceType All}

View Users Mailbox Size in 365/Exchange

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session


get-mailbox | get-mailboxstatistics | ft displayname, totalitemsize

Disable the throttling policy on Exchange

New-ThrottlingPolicy MigrationPolicy

 

Set-ThrottlingPolicy MigrationPolicy -RCAMaxConcurrency $null -RCAPercentTimeInAD $null -RCAPercentTimeInCAS $null -RCAPercentTimeInMailboxRPC $null -EWSMaxConcurrency $null -EWSPercentTimeInAD $null -EWSPercentTimeInCAS $null -EWSPercentTimeInMailboxRPC $null -EWSMaxSubscriptions $null -EWSFastSearchTimeoutInSeconds $null -EWSFindCountLimit $null -CPAMaxConcurrency $null -CPAPercentTimeInCAS $null -CPAPercentTimeInMailboxRPC $null -CPUStartPercent $null

 

Set-Mailbox “<Admin User>” -ThrottlingPolicy MigrationPolicy

Setting Impersonation on Exchange/365

Set-ExecutionPolicy Unrestricted

$LiveCred = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection

Import-PSSession $Session

Enable-OrganizationCustomization

New-ManagementRoleAssignment -Role “ApplicationImpersonation” -User admin@domain.com

Changing Mailbox Receive_Send Size in 365

$credential = Get-Credential

Import-Module MsOnline
Connect-MsolService -Credential $credential

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $credential -Authentication “Basic” -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking

 

Get-Mailbox | Set-Mailbox -MaxReceiveSize 150MB -MaxSendSize 150MB

Removing an Alias from Users

$Records = Get-mailbox -ResultSize Unlimited| where {$_.emailaddresses -like “smtp:*@domain.com”} | Select-Object DisplayName,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -like “smtp:*domain.com”}}}

foreach ($record in $Records)
{
   write-host “Removing Alias” $record.EmailAddresses “for” $record.DisplayName
   Set-Mailbox $record.DisplayName -EmailAddresses @{Remove=$record.EmailAddresses}
}

Removing Groups with a certain Domain from 365

Get-MsolGroup -all | ?{$_.emailaddress -match “domain.com”} | Remove-MsolGroup -force

Spread the word

Share with the Community