Get the Azure Directory Name by PowerShell

Viewed 964

How can I get the Azure Directory(Tenant) name in which I have logged in by PowerShell ?

I have tried 'Get-AzContext', but it only provides the Tenant ID. Tenant name or default domain name is not included in the output.

4 Answers

Get-AzTenant will help.

Get-AzTenant
   [[-TenantId] <String>]
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]

enter image description here


Make sure the version of Az is 4.5.0(the latest version). You could Install-Module -Name Az to update it.

enter image description here

Have you tried the command Get-AzureADTenantDetail

Get-AzureADTenantDetail
   [-All <Boolean>]
   [-Top <Int32>]
   [<CommonParameters>]

You need the AzureAD module for PowerShell and you will get the information by Connecting and then running the Get-AzureADTenantDetail

Install-Module AzureAD
Connect-AzureAD
Get-AzureADTenantDetail

To get the tenant name for the tenant that you have logged in using the Powershell Az module:

$azTenantId = (Get-AzContext).Tenant.Id
$azTenantName = (Get-AzTenant | where-object Id -eq $azTenantId).Name

To get the default tenant domain for the tenant that you have logged in:

$azTenantDomain = Invoke-AzRestMethod `
                    -Method get `
                    -Uri https://graph.microsoft.com/v1.0/domains `
                        | Select-Object -ExpandProperty Content `
                        | Convertfrom-json `
                        | Select-Object -ExpandProperty value `
                        | where-object -Property  isDefault -eq $true `
                        | Select-Object -ExpandProperty id
Related