Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
769 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Could anyone please provide me the Azure PowerShell commands for checking the user belongs to specific tenant and specific subscription?
I tried googling it but didn't find the appropriate command for this requirement.
https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0
but this command will not provide tenant details or subscription details
you can use the following
note: please update according to your requirements
# Install AzureAD and Az modules if not already installed
# Install-Module -Name AzureAD
# Install-Module -Name Az
# Authenticate with the tenant by providing the tenant domain
$tenantDomain = "yourtenant.onmicrosoft.com"
Connect-AzureAD -TenantDomain $tenantDomain
# Get tenant ID
$tenant = Get-AzureADTenantDetail
$tenantId = $tenant.ObjectId
Write-Host "Tenant ID: $tenantId"
# Authenticate with Azure using tenant ID
Connect-AzAccount -Tenant $tenantId
# Specify user and subscription
$userEmail = "user@example.com"
$subscriptionId = "your_subscription_id"
# Get user details
$user = Get-AzureADUser -SearchString $userEmail
if (!$user) {
Write-Host "User not found in the specified tenant"
} else {
Write-Host "User found in the specified tenant"
# Get subscriptions associated with the user
$subscriptions = Get-AzSubscription -User $user.ObjectId
# Check if user belongs to the specified subscription
$userSubscription = $subscriptions | Where-Object { $_.Id -eq $subscriptionId }
if ($userSubscription) {
Write-Host "User belongs to the specified subscription"
} else {
Write-Host "User does not belong to the specified subscription"
}
}