SharePoint Online: Fix “The remote server returned an error (401) Unauthorized” Error in PowerShell

Problem: Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error (401) Unauthorized.” Error in SharePoint Online PowerShell script.

The remote server returned an error: (401) Unauthorized.

Troubleshooting Checklist:

Here are the various root causes and solutions for the remote server returned an error 401 unauthorized in SharePoint Online:

1. Is your URL Correct?

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
 
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
 
#Get the Web
$Web=$Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

You’ll get the same error “Connect-SPOService : The remote server returned an error: (401) Unauthorized.” if you try to connect using the Connect-SPOService cmdlet.

Connect-SPOService -Url "https://crescent-admin.sharepoint.com" -Credential (Get-Credential)

Here in this script, the URL was incorrect! Instead of “https://tenant.sharepoint.com”, it was “http://tenant.sharepoint.com”. The SharePoint Online URL must start with HTTPS always!

2. Do you have Permission to the site?

If you don’t have permission to access the target site through a browser, you can’t access it from PowerShell as well (absolutely!). So, check if you have sufficient permission and are able to open the site in the web browser. If you want to connect to the site using the Connect-SPOService cmdlet, You must also have SharePoint Online administrator role.

3. Legacy authentication protocol is enabled?

Check if the Legacy authentication protocol is enabled in your tenant.

Get-SPOTenant -LegacyAuthProtocolsEnabled

If not, enable it with:

Set-SPOTenant -LegacyAuthProtocolsEnabled $True 

4. Is Custom App Authentication Disabled?

If you are using App authentications, you may get the “The remote server returned an error: (401) Unauthorized.” message when trying to connect using Connect-PnP Online cdmelt. Check if your tenant settings “DisableCustomAppAuthentication” is set to true. If yes, make it false.

Get-SPOTenant -DisableCustomAppAuthentication

Enable custom app authentication with:

Set-SPOTenant -DisableCustomAppAuthentication $False

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

6 thoughts on “SharePoint Online: Fix “The remote server returned an error (401) Unauthorized” Error in PowerShell

  • Hello,
    When I try to add this code into my file
    Set-SPOTenant -LegacyAuthProtocolsEnabled $True
    It returns below:
    Set-SPOTenant : The term ‘Set-SPOTenant’ is not recognized as the name of a cmdlet, function, script file, or operable program.

    Reply
  • thank you very much,

    Reply
  • So do I: Error Generating Site Permission Report! Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (401) Unauthorized.”

    Reply
    • It’s worth checking if you have site collection admin rights on the site. It’s a common misconception that if you have Global Admin rights or a SharePoint Online Admin role, You’ll automatically get admin access to SharePoint! You must add your account as a site collection administrator to each site, still.

      Reply
  • Hello, I still got the same error after executing the command.
    I’m getting The remote server returned an error (401) Unauthorized when trying to create a subsite using powershell CSOM and c# CSOM (Microsoft.SharepointOnline.CSOM)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *