Author : MD TAREQ HASSAN | Updated : 2023/07/25

Connect to azure

# When MFA is enabled (i.e., global admin)
Connect-AzAccount -UseDeviceAuthentication # make sure you are already logged in (default browser)

# When MFA is not Enabled
$azureLoginCredential = Get-Credential -Message "Please Enter Azure Login Credential `r`n(Azure AD Login Id & Password)"
Connect-AzAccount -Credential $azureLoginCredential

Set context (you might have multiple subscriptions)

Set-AzContext -SubscriptionName xyz-subscription

Get susbscription & tenant id

((Get-AzContext).Subscription).Id

(Get-AzTenant).Id

Get current user information

(Get-AzContext).Account

echo ((Get-AzContext).Account).Id

Iterate over subscriptions and resource groups

Get-AzSubscription | ForEach-Object {
    $subscriptionName = $_.Name
    Set-AzContext -SubscriptionId $_.SubscriptionId
    (Get-AzResourceGroup).ResourceGroupName | ForEach-Object {     
        [PSCustomObject] @{
            Subscription = $subscriptionName
            ResourceGroup = $_
        }
    }
}