by

Downloading a shared file from Onedrive for enterprise utilizing Powershell

Downloading a shared file from Onedrive for enterprise utilizing Powershell I believed I would rapidly share this script I got here up with to download a file that was shared utilizing One Drive for Enterprise (which is SharePoint beneath the covers) with Powershell. The next script takes a OneDrive for enterprise URL which might seem like

https://mydom-my.sharepoint.com/private/gscales_domain_com/Paperwork/E-mail%20attachments/filename.txt

This script is fairly easy it makes use of the SharePoint CSOM (Shopper facet object Mannequin) which it masses within the first line. It makes use of the URI object to separate the host and relative URL which the CSOM requires and likewise the SharePointOnlineCredentials object to deal with the Workplace365 SharePoint on-line authentication.

The next script is a perform that take the OneDrive URL, Credentials for Workplace365 and path you wish to download the file to and downloads the file. eg to run the script you’d use one thing like

./spdownload.ps1 ‘https://mydom-my.sharepoint.com/private/gscales_domain_com/Paperwork/E-mail%20attachments/filename.txt

I’ve put a download of the script right here the script itself seems 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_MACHINESOFTWAREMicrosoftSharePoint Shopper Parts'|Type-Object Title -Descending| Choose-Object -First 1 -ExpandProperty Title)).'Location') + "ISAPIMicrosoft.SharePoint.Client.dll")
Add-Sort -Path $SharePointClientDll


perform DownloadFileFromOneDrive{
param (
$DownloadURL = "$( throw 'DownloadURL is a compulsory Parameter' )",
$PSCredentials = "$( throw 'credentials is a compulsory Parameter' )",
$DownloadPath = "$( throw 'DownloadPath is a compulsory Parameter' )"
)
course of{
$DownloadURI = New-Object System.Uri($DownloadURL);
$SharepointHost = "https://" + $DownloadURI.Host
$soCredentials = New-Object Microsoft.SharePoint.Shopper.SharePointOnlineCredentials($PSCredentials.UserName.ToString(),$PSCredentials.password)
$clientContext = New-Object Microsoft.SharePoint.Shopper.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.Shut()
Write-Host ("File downloaded to " + ($destFile))
}
}

$cred = Get-Credential
#exmample use
DownloadFileFromOneDrive -DownloadURL $args[0] -PSCredentials $cred -DownloadPath 'c:Temp'