Automatically detecting Azure or office 365 tenant ID

A simple snippet you can use in your scripts to automatically detect a tenant ID based on your user’s email, login or domain:

$tenantId = (Invoke-RestMethod "https://login.windows.net/$($UserName.Split("@")[1])/.well-known/openid-configuration" -Method GET).userinfo_endpoint.Split("/")[3]

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Jeff Wouters
Jeff Wouters
5 years ago

This is very useful, thanks!!

J. de Groen
J. de Groen
5 years ago

Very useful snippet! Would you know if there is there also is a webrequest from which you could get the tenant name? Like “Contoso B.V.” I looked at the data this request returns, but it is not in there.