How to check a user belongs to multiple Azure AD Tenants using Azure PowerShell?

HKPR 41 Reputation points
2023-04-01T16:00:03.04+00:00

Can we check the user belongs to multiple tenants using PowerShell.

I have tried for the single user but still getting the issues with the below code:

Install-Module -Name AzureAD
Import-Module -Name AzureAD
# Install-Module -Name Az

$tenantDomain1 = "xxx.onmicrosoft.com"
Connect-AzureAD -TenantDomain $tenantDomain1
$tenant = Get-AzureADTenantDetail
$tenantId = $tenant.ObjectId
Write-Host "Tenant ID: $tenantId"

Connect-AzAccount -Tenant $tenantId

$user = Get-AzureADUser -Filter "DisplayName eq 'Samson'"

if (!$user) {
    Write-Host "User not found in the specified tenant"
} else {
    Write-Host "User found in the specified tenant"
}
  1. Could anyone please correct any wrong in above code - that code belongs to checking the user belongs to current tenant.
  2. how to check the same user belongs to multiple tenants using PowerShell query.
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
1,964 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,102 questions
{count} votes