In my code, I am trying to load "web" after creating client context. Below is my code:

$SiteUrl  = "...."; #My site URL goes here#
    $uname =$xmlFile.Account.Trim() 
    $pwd = $xmlFile.Password.Trim()
    Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.dll"
    Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.Runtime.dll"
    $securePassword=New-Object SecureString
    foreach($c in $pwd.ToCharArray())
    # Getting ClientContext object.. 
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($uname, $pwd) 

    $clientContext.Credentials =   New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials)

    if (!$clientContext.ServerObjectIsNull.Value) 

            $web = $clientContext.Web;
            write-host $web




I am getting error:

System.Management.Automation.MethodInvocationException: Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.

Any suggestions?

3 Answers 3


Change the way you load the CSOM DLLs from this:

Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.dll"
Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.Runtime.dll"

to this:

Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'

Substituting whatever the proper path to your DLLs is.

I don't understand exactly what is going on, but in my environment I have to use the full path when running the script on my server, but I don't have to on my workstation. Otherwise I get the same (403) Forbidden error that you do


I think this lines are correct (compare with yours):

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($uname, $pwd) 
$clientContext.Credentials = $credentials 
  • Tried your suggestion as well. No luck! Jan 20, 2017 at 11:24
  • Please check in browser that your user\pass are valid. Jan 20, 2017 at 11:31
  • Yes, its correct. Also,this same code works from one machine. But from different machine(server) its throwing error. Jan 20, 2017 at 11:39
  • Can you confirm there is no proxy configuration for this machine? Jan 20, 2017 at 12:20

Can you try below example and see how it works on your system.

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   

$siteUrl = "site url" 
$username = "username" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Web  

Write-Host $rootWeb.Title  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.