Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (52 sloc) 1.94 KB
This is intended to help when hard matching AD users with Azure AD
Use SkipLogin if you have run the script before on the same session
Andy Morales
#Requires -modules MSOnline
Write-Verbose "Skipping Office 365 login"
else {
Write-Verbose "Asking user for Office 365 credentials"
Write-Host "Enter Office 365 Credentials" -BackgroundColor Yellow -ForegroundColor Black
$Office365Credential = Get-Credential
Write-Verbose "Connecting to Office 365"
$Office365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $Office365Credential -Authentication Basic -AllowRedirection
Import-PSSession $Office365Session
Connect-MsolService -Credential $Office365Credential
[GUID]$UserGuid = (Get-ADUser -Identity $ADUser).ObjectGUID
$bytearray = $UserGuid.tobytearray()
$immutableID = [system.convert]::ToBase64String($bytearray)
Set-MsolUser -UserPrincipalName $O365Email -ImmutableId $immutableID
Get-Mailbox -Identity $O365Email | ForEach-Object {
$ADUserParams = @{
'Identity' = $ADUser;
'EmailAddress' = $_.WindowsEmailAddress;
'add' = @{mailNickname = $_.Alias}
Set-ADUser @ADUserParams
ForEach($address in $_.EmailAddresses) {
Write-Verbose "Adding $address to $_"
Set-ADUser -Identity $ADUser -Add @{proxyAddresses = $address}
catch [System.Management.Automation.ParameterBindingException]{
write-Error "Credentials not received. Please try again."
You can’t perform that action at this time.