Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Created January 6, 2014 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janikvonrotz/8281095 to your computer and use it in GitHub Desktop.
Save janikvonrotz/8281095 to your computer and use it in GitHub Desktop.
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