Forum Discussion
How to uploads files to OneDrive for Business using Powershell
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()
Hi, I tried this script but getting below error:
Exception calling “ExecuteQuery” with “0” argument(s): “The sign-in name or password does not match one in the Microsoft account system.”
But the account do exists.
The account is working if I login through webportal.
- Gerhard_WesselsJun 04, 2019Copper Contributor
Hello, I get the same error.
This worked 'previously' ( until about February this year ).
I am having the same issue with uploading to sharepoint teams sites.
I have tried every piece of code and uploader I could find in the last 2 weeks and nothing is working.
- FlinxDec 18, 2019Copper Contributor
I had to modify this line to make it work
$FileCreationInfo.URL = $File
to this
$FileCreationInfo.URL = $ServerRelativeUrlOfRootFolder + "/" + $File.NameThis needs to be a web relative url and not the file path.
Its should equate to something like:
/personal/your_name_domain_com/Documents/Filename.txt
- Shintaro8088Jan 19, 2024Copper Contributor
How do I use your great code and make it so it that allow me to download users' onedrive for business data to my local drive?
Thanks