Search code examples
azure-powershell

Replace email domain for the guest accounts in Azure AD


I am looking to replace email domain (suffix after @) for the guest users in Azure AD via Powershell. As an example, i need to change email address for jdoe from jdoe@abc to @jdoe.xyz

I tried below command but it is not working.

Get-AzureADUser | where {$.UserType -eq 'Guest' -and $.mail -match "@abc.com"} | ForEach-Object { Set-AzureADUser -ObjectId $($_.ObjectId) -MailNickName "abc@xyz" }

The above might be an incomplete command. Someone suggest what would be the right approach here?


Solution

  • I tried to reproduce the same in my environment to Replace email domain for the guest accounts in Azure AD using Powershell:

    To change the email domain for user accounts that have the account type "guest" in Azure Ad using Powershell, you can use below script.

    1. Connect to Azure AD using connect-AzureAd cmdlet.

    2. Use the below cmdlet to retrieve the user accounts the have account type "guest"

    Get-AzureAdUser -Filter "accountEnabled eq true and userType eq'Guest'"

    enter image description here

    Full PowerShell Script

    Install-Module AzureAD
    Connect-AzureAD
    $guestAccounts = Get-AzureAdUser -Filter "accountEnabled eq true and userType eq 'Guest'"
    foreach ($guestAccount in $guestAccounts) {
    $newUPN = $guestAccount.UserPrincipalName.Replace("@olddomain.com","@newdomain.com")
    Set-AzureADUser -ObjectId $guestAccount.ObjectId -UserPrincipalName $newUPN
    }
    Get-AzureAdUser -Filter "accountEnabled eq true and userType eq 'Guest'"
    

    User UserPrincipal domain changed successfully.

    enter image description here