2

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.

Sourav Karmakar
  • 95
  • 3
  • 12

4 Answers4

2

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

unknown
  • 6,778
  • 1
  • 5
  • 14
0

Have you tried the command Get-AzureADTenantDetail

Get-AzureADTenantDetail
   [-All <Boolean>]
   [-Top <Int32>]
   [<CommonParameters>]
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks Sajeetharan. The above script belongs to AzureRm module, I 'm currently using Az module and can't use AzureRm along with it. – Sourav Karmakar Aug 11 '20 at 07:46
  • @SouravKarmakar You are incorrect, it belongs to the AzureAD module as I showed in my answer above. AzureAD and Az can be used together. :) – Daniel Björk Aug 19 '20 at 10:51
  • Yes @DanielBjörk I was wrong. But, I have got the simpler way to get the work done. Get-AzTenant with the updated Az module has solved my problem. – Sourav Karmakar Aug 19 '20 at 19:01
  • @SouravKarmakar yeah I saw that too, I upvoted that solution since and commented it. :) – Daniel Björk Aug 19 '20 at 19:26
0

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
Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
0

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
mpowrie
  • 603
  • 8
  • 14