Navigate to Site File System in SharePoint Online using PowerShell
Do you know you can navigate to the site file system using SharePoint Online from PowerShell? Well, Here is how it works:
Create a Mapped Drive for SharePoint Online site or Library
To start with, we’ve to map the SharePoint Online library as a drive from PowerShell using the New-PSDrive cmdlet.
#Variable
$LibraryPath = "\\crescentintranet.sharepoint.com@SSL\DavwwwRoot\Sites\marketing"
#Virtually map SharePoint Online Folder as PSDrive
New-PSDrive -name "M" -Root $LibraryPath -PSProvider filesystem
This doesn’t appear in File Explorer, But you can access it through PowerShell. You can also create a persistent drive with -Persist switch.
Access Mapped Drive through PowerShell
Once the library or site is mapped as a network drive, you can navigate the site file system and manipulate it. E.g., let’s copy a folder with its content in a SharePoint Online document library.
#Switch to Mapped Drive
M:
#Change Current Directory to "Branding" Folder
CD Branding
#Get the contents of the Folder
Dir
#Copy a Folder with its contents
Copy 2019 -Destination 2020 -Recurse
Here is the entire flow: