Skip to content

/errors/sharepoint-online/no-connection-available

No connection available. Use Connect-SPOService before running this CmdLet.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
SharePoint Online PowerShell
exit code
1
versions
Microsoft.Online.SharePoint.PowerShell 16.0.27709.12000, Windows PowerShell 5.1
os
windows

No connection available. Use Connect-SPOService before running this CmdLet. is the message of every SharePoint Online Management Shell cmdlet (Get-SPOSite, Set-SPOSite, Set-SPOTenant, ...) when the session has no connection to the SharePoint admin center. Connect with the admin URL of your tenant, then rerun the command:

powershell
Connect-SPOService -Url https://contoso-admin.sharepoint.com
Get-SPOSite -Limit 10

Replace contoso with your tenant name. Only one SharePoint Online connection exists per session; a new Connect-SPOService replaces the old one.

Reproduce it

Module Microsoft.Online.SharePoint.PowerShell 16.0.27709.12000 in Windows PowerShell 5.1, no connection:

powershell
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
try {
  Get-SPOSite -Limit 10 -ErrorAction Stop
} catch {
  $_.Exception.Message
  $_.FullyQualifiedErrorId
}

Output

No connection available. Use Connect-SPOService before running this CmdLet.
System.InvalidOperationException,Microsoft.Online.SharePoint.PowerShell.GetSite

The exception is an InvalidOperationException; in scripts, catch [System.InvalidOperationException] handles it.

Make scripts fail early

If Connect-SPOService fails (wrong URL, cancelled sign-in, missing role), a script without error handling continues and every later cmdlet prints this message. Stop at the first failure:

powershell
try {
  Connect-SPOService -Url https://contoso-admin.sharepoint.com -ErrorAction Stop
} catch {
  Write-Error "SharePoint connection failed: $($_.Exception.Message)"
  exit 1
}

Sources