Forum Discussion
How to use MSgraph for different tenant
Hi, yesterday I managed to get result using this code:
Install-Module Microsoft.Graph -AllowClobber -Force
Import-Module Microsoft.Graph.Users
Connect-MgGraph -Scopes "User.Read.All","AuditLog.Read.All"
Select-MgProfile -Name "beta"
Get-MgUser -Select UserPrincipalName, DisplayName, SignInActivity -Filter "UserType eq 'Member'" -All | Select DisplayName, @{label = "LastSignInDateTime"; Expression = { $_.signInActivity.LastSignInDateTime }}
The thing is, still still works but it gives me the results of the tenant I logged in to.
What do I need to add at the top to make it work, that when running the code it asks me the username and password (again) but that I can enter the credentials of another tenant??
- Use the Disconnect-MgGraph cmdlet to clear the current context, then connect anew. You might need to provide additional parameters depending on your config:
Connect-MgGraph -ContextScope Process -ForceRefresh
2 Replies
- Thiraviam5316Brass Contributor
Nick_DSIt seems that you have used the "Select-MgProfile" cmdlet to select the beta profile. However, in the updated Graph PowerShell module, you can't use "Select-MgProfile" anymore. Instead, you need to install a separate module for accessing the beta cmdlets. For more details regarding this, please refer to:
https://m365scripts.com/microsoft365/install-microsoft-graph-beta-module-discover-the-benefits/ - Use the Disconnect-MgGraph cmdlet to clear the current context, then connect anew. You might need to provide additional parameters depending on your config:
Connect-MgGraph -ContextScope Process -ForceRefresh