I am trying to map a SharePoint online document library to a network drive with PowerShell. It worked when I execute PowerShell with my account but failing when I try to execute same cod in a scheduler job whcih is configured to run as my account

Start-Transcript -Path "$PSScriptRoot\test.log";

$SPLibraryURL ="\\mycompany.sharepoint.com\DavWWWRoot\sites\Sandpit\TEST Incoming Mail"

Write-Host $SPLibraryURL

New-PSDrive -Name "B" -PSProvider FileSystem -Root $SPLibraryURL

$sourceRiskFile = Get-ChildItem "B:"

Foreach ($file in $sourceRiskFile)

 Write-Host $file.Name



I have also tried this URL but no success


Error I am getting is

New-PSDrive : The specified drive root "https://mycompany.sharepoint.com/sites/Sandpit/TEST%20Incoming%20Mail" either does not exist, or it is not a folder.

job configuration is

enter image description here

There must be something to do with Scheduler job as same code works fine if I run PowerShell directly.

2 Answers 2


I don't think you will ever be able to do this. Several reasons:

  • Even when doing this manually, your mapping will stop working from time to time. For example when you log out.
  • You need to open the site you map using Internet Explorer
  • Your scheduled script won't be able to open the Internet Explorer with the UI. Especially when you "run as" in your script.
  • Many tried to do what you are doing and failed. You could do this with SP on-prem though, but it won't work with SPO.

If you want users to use a mapped folder, then it's a bad idea. WebDav protocol has many issues: it's very slow, it does not support collaboration for Office documents, it has issues with SharePoint columns, etc. Instead, I recommend using the OneDrive sync tool. Although, you will have to ask users to sync the library manually.


In your scheduled job, you want to run a PowerShell script that connects to SharePoint Online and downloads a file. You can use PowerShell Pnp for this purpose.

  • I am currently doing it with PNP but I have to either sore credentials in the script or in Windows Password Manager where anyone can go and edit, is there any better way of doing it?
    – Ali
    Mar 11, 2021 at 6:23

Alternately, you could use Edge Browser and Selenium to automate mapping your Sharepoint drive

Selenium Powershell Module

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.