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
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
I’ve put a download of the script right here the script itself seems like
$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
$DownloadURL = "$( throw 'DownloadURL is a compulsory Parameter' )",
$PSCredentials = "$( throw 'credentials is a compulsory Parameter' )",
$DownloadPath = "$( throw 'DownloadPath is a compulsory Parameter' )"
$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);
Write-Host ("File downloaded to " + ($destFile))
$cred = Get-Credential
DownloadFileFromOneDrive -DownloadURL $args -PSCredentials $cred -DownloadPath 'c:Temp'