/errors/microsoft-graph/authentication-needed-call-connect-mggraph
Authentication needed. Please call Connect-MgGraph.
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
- tool
- Microsoft Graph PowerShell
- exit code
- 1
- versions
- Microsoft.Graph 2.x (checked with 2.41.2), Windows PowerShell 5.1, PowerShell 7
- os
- windows, linux, macos
Authentication needed. Please call Connect-MgGraph. means a Microsoft Graph PowerShell cmdlet (Get-MgUser, Get-MgGroup, New-MgUser, ...) was called without a Graph sign-in in this session. Connect first, requesting the permissions the cmdlet needs:
Connect-MgGraph -Scopes "User.Read.All"
Get-MgUser -Top 5Reproduce it
Microsoft.Graph.Users 2.41.2 imported, no connection:
Import-Module Microsoft.Graph.Users
try {
Get-MgUser -Top 5 -ErrorAction Stop
} catch {
$_.Exception.Message
$_.FullyQualifiedErrorId
}
'Context present: ' + [bool](Get-MgContext)Output
Authentication needed. Please call Connect-MgGraph. Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List Context present: False
Get-MgContext returns nothing when there is no connection, which makes it the check to use in scripts.
In scripts
if (-not (Get-MgContext)) {
Connect-MgGraph -Scopes "User.Read.All" -NoWelcome -ErrorAction Stop
}For unattended scripts, connect with app-only authentication (-ClientId, -TenantId, -CertificateThumbprint, or -Identity for a managed identity) at the top of the script. See Connect-MgGraph -Scopes for the options.
Still failing after connecting
- The cmdlet runs in a different runspace (
Start-Job,ForEach-Object -Parallel): connect inside it. - You connected with
-ContextScope Processin another window: the defaultCurrentUserscope caches the sign-in for your user,Processkeeps it in one session only. - Several versions of the Microsoft.Graph modules are installed and different versions load (
Get-Module Microsoft.Graph*shows them). Load the same version of Microsoft.Graph.Authentication and the other Graph modules.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.graph.authentication/connect-mggraph
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/microsoftgraph/authentication-commands
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.graph.authentication/get-mgcontext
Related pages
commands · microsoft-graphConnect-MgGraph -Scopes: which permissions to request in Microsoft Graph PowerShellConnect-MgGraph -Scopes requests delegated Graph permissions such as User.Read.All. Find the scope a cmdlet needs with Find-MgGraphCommand; app-only differs.commands · microsoft-graphGet-MgContext: check the Microsoft Graph PowerShell connection, account, tenant and scopesGet-MgContext shows the account, tenant, auth type and scopes of the current Microsoft Graph PowerShell session, and returns nothing when you are not connected.errors · microsoft-graphMicrosoft Graph PowerShell: Insufficient privileges to complete the operation (Authorization_RequestDenied)Graph returned 403: the token lacks the permission the call needs, or the user lacks the Entra role. Reconnect with the right -Scopes or grant the app role.