Skip to content

/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:

powershell
Connect-MgGraph -Scopes "User.Read.All"
Get-MgUser -Top 5

Reproduce it

Microsoft.Graph.Users 2.41.2 imported, no connection:

powershell
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

powershell
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 Process in another window: the default CurrentUser scope caches the sign-in for your user, Process keeps 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