Living a SharePoint life

Tuesday, January 17, 2017

Connect to Office365 via Office PnP behind a proxy

How do you connect to your SharePoint Office365 tenant with the PowerShell if you are locked behind a proxy? This hint will probably work for all kind of PowerShell scripts, not only for Office PnP. I tried this with the Microsoft SharePoint Online extensions as well and it worked.

So my problem was, the company I was working for, had a proxy which could not be bypassed. The proxy settings have been set by a GPO and the network settings where fine so far. However the proxy used user authentication to access the internet. Now if I want to use the SharePoint Online tools, I must authenticate my PowerShell session at the proxy so the tools can access the internet too.

The trick to get your PowerShell connected is quite simple. You only need to enter the following line:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

Now you should be able to connect to Office365 SharePoint without problems. If you don't want to enter this line every time you open a new PowerShell, you could add it to your PowerShell profile.

then add this to the profile file:

#Set user default credentials for any webrequest
"Setting the users default credentials for WebRequests..." | Write-Host -ForegroundColor White
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials


  1. Trying to connect to may site collection ( SP2016) but through a proxy(

    $cred = [System.Net.CredentialCache]::DefaultCredentials
    [System.Net.WebRequest]::DefaultWebProxy.Credentials = $cred
    Connect-PnPOnline -Url https://xxx -UseWebLogin
    he problem is no connection was made:

    Get-PnPWeb or Get-PnPSite is null

    Get-PnPSite : Cannot contact site at the specified URL https://xxx/. Au caractère Ligne:1 : 1 + Get-PnPSite + ~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Get-PnPSite], ClientRequestException + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Site.GetSite

    1. As long as you havn't connected to the site thru Connect-PnPOnline, every other PnP cmdlet will fail and raise an exception.
      The PowerShell line [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials will set the context of the user running the PowerShell to be used for any kind of http connection estabished thru System.Net.Webrequest. Possible problems may be the user isn't allowd to login to the proxy, or the proxy does deep packet inspections and SSL termination to block unauthorized traffic.


Featured Post

The Retro Powershell - Looking good in 8-Bit | Part 1

I wrote a little script that, when placed in your PowerShell Profile, will print a message similar to the old boot message you got from you...