Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greg-fischer/d92c72f9d2931ba90964fcb02a2a6f0d to your computer and use it in GitHub Desktop.
Save greg-fischer/d92c72f9d2931ba90964fcb02a2a6f0d to your computer and use it in GitHub Desktop.
O365 User Creation for GSuite Migration - Powershell
# Use While following this Doc: https://docs.microsoft.com/en-us/exchange/mailbox-migration/perform-g-suite-migration
# start powershell as administrator first!!!
# First time running Exchange stuff you need modules installed, do this:
# Install-Module MSOnline
# !! This is not a complete script, run in sections!!
# Run this first section to connect to O365 , then run the users
# Maybe even run a few users at a time.
# I find it easier to test and deal with errors in batches
# Just copy and paste code in chunks.
# ------------------- Do first connection stuff
# Set your primary/root domain
$domain = "yourdomain.com"
Set-ExecutionPolicy RemoteSigned
$Cred = Get-Credential
Connect-MsolService -Credential $Cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
# Once you authenticate and connect, you'll get a few warnings and some output similar to this
# ModuleType Version Name ExportedCommands
# ---------- ------- ---- ----------------
# Script 1.0 tmp_z443333a.nex {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, Add-MailboxFolderPermission, Add-MailboxLocation...}
# ------------------- Now do any User creation stuff
# NOTE: $user_pw MUST USE SINGLE QUOTE!! for string literal
# Note: -ExternalEmailAddress $gsemail is added in the command but it did not work for me.
# This should have added the reverse forwarder on the account, but didn't work
# so I had to run the second set of scripting below to added forarders.
# After adding a user, you should not get any errors and it shows simple mailuser properties
# Name RecipientType
# ---- -------------
# Brian Hobart MailUser
# Per-User properties (User 1)
$first_name = "Brian"
$last_name = "Hobart"
$user_pw = 'Your<>pass1234'
# Exchange Alias Name (user name only)
$exchange_alias = "brian_hobart"
# Users mailbox name and display name
$mbox = "${first_name} ${last_name}"
#Write-Host $mbox
# Users primary email address (same on GS and MS)
$primary_email = "${exchange_alias}@${domain}"
# GSuite subdomain address
$gsemail = "${exchange_alias}@gsuite.${domain}"
# MS O365 subdomain address
$msemail = "${exchange_alias}@o365.${domain}"
# Add new accounts as Exchange Admin / MailUser
New-MailUser -Name $mbox -Alias $exchange_alias -firstname $first_name -lastname $last_name -DisplayName $mbox -ExternalEmailAddress $gsemail -MicrosoftOnlineServicesID $primary_email -Password (ConvertTo-SecureString -String $user_pw -AsPlainText -Force)
# Add the email alias for o365 subdomain
Set-MailUser $mbox -EmailAddresses @{add=$msemail}
# NOTE: THis should be run separately... but I wanted to show here anyway
# After setup accounts , you need to run after adding user license, then you can run this
# This wont work because migration hasn't started and I'm pretty sure it wouldn't even work
# until a license has been added to the account.
# This adds the forwarding address back to GSuite subdomain for users not migrated yet
# Completion of batch removes this forwarder (I think)
# Set-Mailbox -Identity $mbox -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $gsemail
# Get-Mailbox $mbox | fl name,forwardingSMTPAddress,delivertomailboxandforward
# Per-User properties (User 2)
$first_name = "Dwight"
$last_name = "Shrute"
$user_pw = 'Your<>pass1234'
# Exchange Alias Name (user name only and user name portion of email address)
$exchange_alias = "dwight_shrute"
# Users mailbox name and display name
$mbox = "${first_name} ${last_name}"
#Write-Host $mbox
# Users primary email address (same on GS and MS)
$primary_email = "${exchange_alias}@${domain}"
# GSuite subdomain address
$gsemail = "${exchange_alias}@gsuite.${domain}"
# MS O365 subdomain address
$msemail = "${exchange_alias}@o365.${domain}"
# Add new accounts as Exchange Admin / MailUser
New-MailUser -Name $mbox -Alias $exchange_alias -firstname $first_name -lastname $last_name -DisplayName $mbox -ExternalEmailAddress $gsemail -MicrosoftOnlineServicesID $primary_email -Password (ConvertTo-SecureString -String $user_pw -AsPlainText -Force)
# Add the email alias for o365 subdomain
Set-MailUser $mbox -EmailAddresses @{add=$msemail}
# NOTE: THis should be run separately... but I wanted to show here anyway
# After setup accounts , you need to run after adding user license, then you can run this
# This wont work because migration hasn't started and I'm pretty sure it wouldn't even work
# until a license has been added to the account.
# This adds the forwarding address back to GSuite subdomain for users not migrated yet
# Completion of batch removes this forwarder (I think)
# Set-Mailbox -Identity $mbox -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $gsemail
# Get-Mailbox $mbox | fl name,forwardingSMTPAddress,delivertomailboxandforward
# ==================== After batch has been started, AND license added to account
# You can run this in same session, just not until
# the account shows up in O365 Admin and you add a license
# This adds the forwarding address back to GSuite subdomain for users not migrated yet
# Completion of batch removes this forwarder (I think)
# Note, this does NOT keep a local copy when forwarded
# Set your primary/root domain
$domain = "yourdomain.com"
# User 1
# GSuite subdomain address
$gsemail = "brian_hobart@gsuite.${domain}"
$mbox = "Brian Hobart"
Set-Mailbox -Identity $mbox -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $gsemail
Get-Mailbox $mbox | fl name,forwardingSMTPAddress,delivertomailboxandforward
# User 2
# GSuite subdomain address
$gsemail = "dwight_shrute@gsuite.${domain}"
$mbox = "Dwight Shrute"
Set-Mailbox -Identity $mbox -DeliverToMailboxAndForward $false -ForwardingSMTPAddress $gsemail
Get-Mailbox $mbox | fl name,forwardingSMTPAddress,delivertomailboxandforward
# ------------------- Final commands, optional
# List all accounts with forwarders
Get-Mailbox | Where {$_.ForwardingSMTPAddress -ne $null} | Select Name, UserPrincipalName, ForwardingAddress, ForwardingSMTPAddress, DeliverToMailboxAndForward
# remove session
Remove-PSSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment