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.

  2. I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. Office video intercom installation

  3. This short article posted only at the web site is truly good.

  4. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. 1337x

  5. Thanks to innovations in Internet technology, it is a lot easier (and often more profitable) to utilize the services of a remote workforce to handle a business's many tasks mindmap templates

  6. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. via4d


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...