Retrieving Microsoft 365 Tenant ID with PowerShell
A practical guide for network engineers to quickly retrieve a Microsoft 365 Tenant ID using the Microsoft Graph PowerShell SDK for automation and API integrations.
If you work with Microsoft 365, Azure, or Microsoft Graph, you will inevitably be asked for your Tenant ID. While you can dig through the Azure Portal to find it, sometimes you just need a quick, scriptable way to grab it directly from your command line.
In this post, we'll cover what a Tenant ID is, why you might need it, and how to use a simple Microsoft Graph PowerShell script to retrieve it instantly.
What is a Tenant ID?
A Tenant ID is a Globally Unique Identifier (GUID) that represents your organization's specific instance of Microsoft Entra ID (formerly known as Azure Active Directory). When your organization signs up for a Microsoft cloud service like Microsoft 365, Microsoft Intune, or Azure, a dedicated and isolated "tenant" is created. The Tenant ID is the 32-character alphanumeric string that serves as the digital address for that specific environment.
Why is it Required?
You will frequently need your Tenant ID for a variety of administrative and developmental tasks, including:
- Configuring Enterprise Applications: Setting up Single Sign-On (SSO) or OAuth 2.0 integrations often requires specifying the exact tenant.
- API Integrations: When writing custom code or scripts that interact with the Microsoft Graph API or Azure Resource Manager.
- Infrastructure as Code (IaC): Deploying resources via Terraform, Bicep, or ARM templates usually requires referencing the Tenant ID.
- Microsoft Support: Opening high-level technical support tickets often requires providing your Tenant ID so engineers can locate your environment.
Prerequisites
Before running the script, you need to ensure your environment is set up to interact with Microsoft Graph. You will need the Microsoft Graph PowerShell SDK installed—specifically, the authentication module.
Open PowerShell as an Administrator and run the following command to install the module:
Install-Module Microsoft.Graph.Authentication -Scope CurrentUser -Force
The PowerShell Script
The following script handles connecting to Microsoft Graph, extracting the Tenant ID from your current session context, and displaying it clearly. It includes a pause at the end to ensure you have time to copy the ID before the window closes.
#Requires -Modules Microsoft.Graph.Authentication
# Set Error Action Preference
$ErrorActionPreference = "Stop"
try {
Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Cyan
# Connect to MS Graph (Interactive browser login)
Connect-MgGraph -NoWelcome
# Retrieve the current connection context
$MgContext = Get-MgContext
if ($MgContext.TenantId) {
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "Tenant ID successfully retrieved!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "Tenant ID: " -NoNewline
Write-Host "$($MgContext.TenantId)" -ForegroundColor Yellow
Write-Host "========================================`n" -ForegroundColor Green
} else {
Write-Host "`n[!] Could not retrieve Tenant ID from the current context." -ForegroundColor Yellow
}
} catch {
Write-Host "`n[X] An error occurred while running the script:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
} finally {
# Ensure the script does not close immediately
Write-Host ""
Read-Host "Press [Enter] to exit"
}
How to Run the Script
- Save the Code: Copy the script above and paste it into a code editor like Visual Studio Code or PowerShell ISE.
- Save the File: Save the file with a .ps1 extension. For example, name it Get-TenantId.ps1.
- Execute the Script:
- Open PowerShell.
- Navigate to the folder where you saved the script using the
cdcommand (e.g.,cd C:\Scripts). - Run the script by typing
.\Get-TenantId.ps1and pressing Enter.
- Authenticate: A web browser window will pop up prompting you to sign in. Log in with your administrative credentials.
- Copy Your ID: Your Tenant ID will be displayed in yellow text. Highlight and copy it, then press Enter to exit.