I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like
https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt
This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication.
The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like
./spdownload.ps1 'https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt'
I've put a download of the script here the script itself looks like
https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt
This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication.
The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like
./spdownload.ps1 'https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt'
I've put a download of the script here the script itself looks like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $SharePointClientDll = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SharePoint Client Components\'|Sort-Object Name -Descending| Select-Object -First 1 -ExpandProperty Name)).'Location') + "ISAPI\Microsoft.SharePoint.Client.dll") Add-Type -Path $SharePointClientDll function DownloadFileFromOneDrive{ param ( $DownloadURL = "$( throw 'DownloadURL is a mandatory Parameter' )", $PSCredentials = "$( throw 'credentials is a mandatory Parameter' )", $DownloadPath = "$( throw 'DownloadPath is a mandatory Parameter' )" ) process{ $DownloadURI = New-Object System.Uri($DownloadURL); $SharepointHost = "https://" + $DownloadURI.Host $soCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($PSCredentials.UserName.ToString(),$PSCredentials.password) $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SharepointHost) $clientContext.Credentials = $soCredentials; $destFile = $DownloadPath + [System.IO.Path]::GetFileName($DownloadURI.LocalPath) $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($clientContext, $DownloadURI.LocalPath); $fstream = New-Object System.IO.FileStream($destFile, [System.IO.FileMode]::Create); $fileInfo.Stream.CopyTo($fstream) $fstream.Flush() $fstream.Close() Write-Host ("File downloaded to " + ($destFile)) } } $cred = Get-Credential #exmample use DownloadFileFromOneDrive -DownloadURL $args[0] -PSCredentials $cred -DownloadPath 'c:\Temp\' |