OneDrive for Business - Upload document using PowerShell / CSOM

%3CLINGO-SUB%20id%3D%22lingo-sub-70826%22%20slang%3D%22en-US%22%3EOneDrive%20for%20Business%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-70826%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20upload%20a%20file%20(actually%20copy%20a%20file%20from%20a%20SP%20site)%20to%20the%20default%20Documents%20list%20in%20OneDrive%20for%20Business%26nbsp%3Bbut%20the%20Copy-PnPFile%20-SourceUrl%20%24source%20-TargetUrl%20%24target%20cmdlet%20is%20expecting%20a%20relative%20path%20versus%20an%20absolute%20path%20to%20%3CA%20href%3D%22https%3A%2F%2Ftenant-my.sharepoint.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenant-my.sharepoint.com%3C%2FA%3E%20%E2%80%A6%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUnfortunately%20I%20have%20not%20been%20able%20to%20find%20any%20PowerShell%20examples%20that%20can%20upload%20a%20file%20to%20OneDrive.%20Is%20it%20even%20possible%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETim%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-70871%22%20slang%3D%22en-US%22%3ERe%3A%20OneDrive%20for%20Business%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-70871%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you.%20Vesa%20also%20pointed%20me%20to%20the%20fact%20that%20I%20could%20connect%20to%20the%20OD4B%20site%20directly%20using%20the%20connect-pnponline%20PnP%20cmdlet%20and%20then%20use%20the%26nbsp%3BAdd-PnPFile%20cmdlet%20to%20upload%20the%20file.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Econnect-pnponline%20-Url%20%24OneDriveURL%20%E2%80%93Credentials%20(Get-Credential)%3CBR%20%2F%3EAdd-PnPFile%20-Path%20%24Source%20-Folder%20%22Documents%22%20-Values%20%40%7BModified%3D%225%2F1%2F2017%22%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPFile.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%2Fblob%2Fmaster%2FDocumentation%2FAddPnPFile.md%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-70848%22%20slang%3D%22en-US%22%3ERe%3A%20OneDrive%20for%20Business%20-%20Upload%20document%20using%20PowerShell%20%2F%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-70848%22%20slang%3D%22en-US%22%3EThis%20is%20an%20old%20Script%2C%20but%20it%20should%20work%3A%20%3CA%20href%3D%22https%3A%2F%2Fblogs.technet.microsoft.com%2Ffromthefield%2F2014%2F02%2F19%2Foffice-365-powershell-script-to-upload-files-to-a-document-library-using-csom%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.technet.microsoft.com%2Ffromthefield%2F2014%2F02%2F19%2Foffice-365-powershell-script-to-upload-files-to-a-document-library-using-csom%2F%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3ECheers%3C%2FLINGO-BODY%3E
Microsoft

I am trying to upload a file (actually copy a file from a SP site) to the default Documents list in OneDrive for Business but the Copy-PnPFile -SourceUrl $source -TargetUrl $target cmdlet is expecting a relative path versus an absolute path to https://tenant-my.sharepoint.com

 

Unfortunately I have not been able to find any PowerShell examples that can upload a file to OneDrive. Is it even possible?

 

Tim

2 Replies

Thank you. Vesa also pointed me to the fact that I could connect to the OD4B site directly using the connect-pnponline PnP cmdlet and then use the Add-PnPFile cmdlet to upload the file.

 

connect-pnponline -Url $OneDriveURL –Credentials (Get-Credential)
Add-PnPFile -Path $Source -Folder "Documents" -Values @{Modified="5/1/2017"}

 

https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPFile.md