Get the Azure Directory Name by PowerShell

2.3k Views Asked by At

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.

5

There are 5 best solutions below

4
unknown On BEST ANSWER

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

4
Sajeetharan On

Have you tried the command Get-AzureADTenantDetail

Get-AzureADTenantDetail
   [-All <Boolean>]
   [-Top <Int32>]
   [<CommonParameters>]
0
Daniel Björk On

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
0
mpowrie On

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
0
David Homer On

I've just run into this - the problem may also be (and was for me) if you're signed in with a service principal with certificate it doesn't have the tenant information in Get-AzTenant (because the REST API itself doesn't provide the information to service principals).

Like mpowrie said you need to switch to Graph API.