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:

When working with SharePoint Online using PowerShell, you may encounter the error “The remote server returned an error (401) Unauthorized.” This error can occur when the script is not properly authenticated to access the SharePoint Online site. In this tutorial, we will discuss how to fix the “The remote server returned an error (401) Unauthorized” error.

Here are the various root causes and solutions for the remote server returning 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 access the site?

If you don’t have permission to access the target site through a browser, you can’t access it from PowerShell (absolutely!). So, check if you have sufficient permission and can 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 the following:

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 the Connect-PnP Online cmdlet. 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

Summary

In summary, The “The remote server returned an error (401) Unauthorized” error in PowerShell when working with SharePoint Online can be caused by several factors, such as incorrect credentials or a lack of permissions. To fix this error, you can try several methods explained above. It’s also important to ensure that the credentials you are using have the necessary permissions to access the SharePoint Online site. By following these steps, you can successfully fix the “The remote server returned an error (401) Unauthorized” error and continue to work with SharePoint Online using PowerShell.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • Hi Salaudeen – i get an error message when running the PS commands in step 2 & 3.
    Where might the UI settings be for these? Screen shots please.

    Reply
  • 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 *