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]
This is very useful, thanks!!
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.