SharePoint Online: Upload File to Sub-Folder using PowerShell
Requirement: SharePoint Online PowerShell to Upload File to Folder.
PowerShell to Upload File to SharePoint Online Sub-Folder
Here is the PowerShell to upload file to SharePoint Online Document library subfolder.
Upload File to SharePoint Online Folder using PnP PowerShell
To upload file to a folder in SharePoint Online document library using PnP PowerShell, use:
If you need to upload all files and folders from a local drive to SharePoint Online using PowerShell, use: SharePoint Online: Upload All Files & Sub-Folders using PowerShell
PowerShell to Upload File to SharePoint Online Sub-Folder
Here is the PowerShell to upload file to SharePoint Online Document library subfolder.
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Set parameter values $SiteURL="https://crescent.sharepoint.com/sites/marketing" $SourceFilePath="C:\Users\salaudeen\Desktop\Project Documents\Discloser Asia.doc" $TargetFolderRelativeURL ="/sites/Marketing/Shared Documents/2018/Active" #Setup Credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) Try { #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Credentials #Get the Target Folder to upload $Web = $Ctx.Web $Ctx.Load($Web) $TargetFolder = $Web.GetFolderByServerRelativeUrl($TargetFolderRelativeURL) $Ctx.Load($TargetFolder) $Ctx.ExecuteQuery() #Get the source file from disk $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead() #Get File Name from source file path $SourceFileName = Split-path $SourceFilePath -leaf $TargetFileURL = $TargetFolderRelativeURL+"/"+$SourceFileName #Upload the File to SharePoint Library Folder $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.ContentStream = $FileStream $FileCreationInfo.URL = $TargetFileURL $FileUploaded = $TargetFolder.Files.Add($FileCreationInfo) $Ctx.ExecuteQuery() #Close file stream $FileStream.Close() Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green } catch { write-host "Error Uploading File to Folder: $($_.Exception.Message)" -foregroundcolor Red }This PowerShell uploads file to SharePoint Online document library sub-folder.
Upload File to SharePoint Online Folder using PnP PowerShell
To upload file to a folder in SharePoint Online document library using PnP PowerShell, use:
#Config Variables $SiteURL = "https://crescenttech.sharepoint.com" $SourceFilePath ="C:\Documents\SRS.Docx" $DestinationFolderPath = "/Shared Documents/2018" #Get Credentials to connect $Cred = Get-Credential Try { #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials $Cred #Upload File to Folder Add-PnPFile -Path $SourceFilePath -Folder $DestinationFolderPath -ErrorAction Stop } catch { write-host "Error: $($_.Exception.Message)" -foregroundcolor Red }The Add-PnPFile cmdlet also supports these parameters: NewFileName, Checkout, Approve, Publish, ContentType, Values.
If you need to upload all files and folders from a local drive to SharePoint Online using PowerShell, use: SharePoint Online: Upload All Files & Sub-Folders using PowerShell
#i have created new script to upload files in subfolder in sharepoint
ReplyDelete#Specify tenant admin and site URL
$User = "your user_name"
$SiteURL = "https://*****.sharepoint.com"
#local pc folder
$Folder = "C:\dest\"
$DocLibName = "Documents"
#folder of sharepoint where you want to copy the file or folder
$FolderName = "Data_Center/Backups/HRMS/"
#install sharepoint sdk on your machine and give path of this in below code
#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"
#your password
$Password = "********" | ConvertTo-SecureString -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.RootFolder)
$Context.ExecuteQuery()
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName);
#Upload file(s)
Foreach ($File in (dir $Folder -File))
{
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
$FileCreationInfo.URL = $File.Name
$UploadFile = $TargetFolder.Files.Add($FileCreationInfo)
$Context.Load($UploadFile)
$Context.ExecuteQuery()
}