How can I prevent getting prompted multiple times when executing Microsoft Graph Powershell script ?

EnterpriseArchitect 4,891 Reputation points

How do I avoid being prompted multiple times when running the Microsoft Graph Powershell script?

Connect-MgGraph -Scopes Reports.Read.All, User.Read.All, UserAuthenticationMethod.Read.All
Select-MgProfile beta

Get-MgReportAuthenticationMethodUserRegistrationDetail -Filter "IsMfaRegistered eq false and IsAdmin eq true"

With the above script, there are 40+ users returned; hence I was prompted 40 times to login with the MFA. Any assistance would be greatly appreciated.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,889 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,176 questions
{count} votes

Accepted answer
  1. Andy David - MVP 142.8K Reputation points MVP

    Try the solution here:

    • Customers upgrading from previous versions of the SDK may encounter auth prompts on every command call. If this happens, one can use the following steps to reset their token cache:
    • Use Disconnect-MgGraph to sign out of the current session.
    • Run Remove-Item "$env:USERPROFILE\.graph" -Recurse -Force to delete your token cache.
    • Run Connect-MgGraph to reconstruct a clean token cache.
    7 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 44,016 Reputation points

    Hello there, You can use an unattended daemon, which requires connecting to MS graph using the application's client id and certificate name. Connect-MgGraph -ClientID YOUR_APP_ID -TenantId YOUR_TENANT_ID -CertificateName YOUR_CERT_SUBJECT
    If Similar discussion here Hope this resolves your Query !! --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments