Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell: Update Obsolete User Principal Names in Office 365 Windows Azure Directory #PowerShell #Office365 #ActiveDirectory
#--------------------------------------------------#
# settings
#--------------------------------------------------#
$OU = "OU=vblusers2,DC=vbl,DC=ch"
#--------------------------------------------------#
# modules
#--------------------------------------------------#
Import-Module MSOnline
Import-Module MSOnlineExtended
Import-Module ActiveDirectory
#--------------------------------------------------#
# main
#--------------------------------------------------#
$ADUsers = Get-ADUser -Filter * -SearchBase $OU -Properties GivenName, Surname, DisplayName
$Credential = Import-PSCredential $(Get-ChildItem -Path $PSconfigs.Path -Filter "Office365.credentials.config.xml" -Recurse).FullName
Connect-MsolService -Credential $Credential
$MsolUsers = Get-MsolUser -All
$MsolUsers | %{
$MsolUser = $_
$ADUsers | where{($_.GivenName -eq $MsolUser.FirstName) -and
($_.Surname -eq $MsolUser.LastName) -and
($_.DisplayName -eq $MsolUser.DisplayName) -and
($_.UserPrincipalName -ne $MsolUser.UserPrincipalName)
} | %{
Write-Host "Change UserPrincipalName for: $($MsolUser.UserPrincipalName) to: $($_.UserPrincipalName)"
Set-MsolUserPrincipalName -UserPrincipalName $MsolUser.UserPrincipalName -NewUserPrincipalName $_.UserPrincipalName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.