Forum Discussion
Uday Kiran Reddy
Dec 12, 2017Copper Contributor
How to uploads files to OneDrive for Business using Powershell
I tried the sample powershell code in below link, getting error as path not found.
https://community.spiceworks.com/topic/2001221-powershell-script-to-upload-file-to-onedrive-business
Sam...
Manidurai Mohanamariappan
Dec 15, 2017Iron Contributor
you can try this modified script and this script support sub folder in document library
$User = "testuser@domainname.com"
$SiteURL = "https://test-my.sharepoint.com/personal/testuser_domainame_com";
$Folder = "C:\Users\Desktop\New folder"
#DocDocLibName is document libary name
$DocLibName = "Documents"
$foldername = "Attachments"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = ConvertTo-SecureString ‘test1234’ -AsPlainText -Force
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
#Retrieve list
$List = $Context.Web.Lists.GetByTitle("$DocLibName")
$Context.Load($List)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$ServerRelativeUrlOfRootFolder = $List.RootFolder.ServerRelativeUrl
$uploadFolderUrl= $ServerRelativeUrlOfRootFolder+"/"+$foldername
#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
if($foldername -eq $null)
{
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
}
Else
{
$targetFolder = $Context.Web.GetFolderByServerRelativeUrl($uploadFolderUrl)
$Upload = $targetFolder.Files.Add($FileCreationInfo);
}
#$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
javier_perez
Sep 20, 2019Copper Contributor
thanks for your code...
but, how can i do the same uploading to folders instead of lists?
regards