0

I have multiple main folders on SharePoint online each with their own sets of sub-folders and files. I need to delete a specific folder (and its full contents including any sub-folders and files) from each of those main folders. The folder has the same name across all main folders, but obviously it does not share the same filepath/url. This is what I have come up with so far from the research I have done. The code for deleting the folder is not even running as far as I can tell.  Any and all help is greatly appreciated.

$url_sharepoint = "https://company.sharepoint.com"
$library = "Sharepoint Library"
$deleteFolders = "Folder Name"
$source_folder = "Network Share:\filepath"

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePassword)
$clientContext.Credentials = $cred

$mylist = $clientContext.web.Lists.GetByTitle($library)
$clientContext.Load($mylist)
$clientContext.ExecuteQuery()

#Get name of folder
$MainFolder = Split-Path $source_folder -Leaf

#upload folder to sharepoint
$UploadMainFolder = $mylist.RootFolder.Folders.Add($MainFolder)
$clientContext.Load($UploadMainFolder)
$clientContext.ExecuteQuery()

$folders = $UploadMainFolder.Folders[$library].SubFolders

Foreach($folder in $folders)
{
    if ($folder.Name -match $deleteFolders)
    {
        $UploadMainFolder.Folders[$library].SubFolders.Delete($folder)
        Write-Host "Folder has been deleted!"
    }
    else
    {
        Write-Host "Folder has not been deleted!"
    }
}
  • Hi Tom, Have you tried the code snippet below ? Did it work for you ? – Jerry_MSFT Jan 17 at 0:44
1

The CSOM Method to delete the folder is DeleteObject(), for example:

$targetFolder = $clientContext.Web.GetFolderByServerRelativeUrl($folderRelativeUrl); $targetFolder.DeleteObject(); $clientcontext.ExecuteQuery();

I would also say overall your script is doing things the hard way. You don't have to re-invent the PowerShell wheel. You should install the Microsoft SharePoint Online PowerShell Module (installation instructions here and reference lookup here) and the Office PnP PowerShell Module (installation instructions here and reference lookup here) in your PowerShell window, where a lot of what you are trying to do has pre-built cmdlets to help. These are essential for doing PowerShell against SharePoint Online.

| improve this answer | |
  • Thanks, everyone. Your suggestions pointed me in the right direction. – Tomcat1208 Jan 17 at 12:46
1

Modify the script as below:

$User = "user@tenant.onmicrosoft.com"  
$Password = '*****'  
$url_sharepoint = "https://zheguo.sharepoint.com"
$library = "doc1"
$deleteFolders = "TestFolder"
$source_folder = "C:\TestFolder"
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"

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url_sharepoint)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Password -AsPlainText -Force)) 
$clientContext.Credentials = $cred

$mylist = $clientContext.web.Lists.GetByTitle($library)
$clientContext.Load($mylist)
$clientContext.ExecuteQuery()

#Get name of folder
$MainFolder = Split-Path $source_folder -Leaf

#upload folder to sharepoint
$UploadMainFolder = $mylist.RootFolder.Folders.Add($MainFolder)
$clientContext.Load($UploadMainFolder)
$clientContext.ExecuteQuery()

$folders = $mylist.RootFolder.Folders;
$clientContext.Load($folders);
$clientContext.ExecuteQuery();
Foreach($folder in $folders)
{
    if ($folder.Name -match $deleteFolders)
    {
        $Clientcontext.Web.GetFolderByServerRelativeUrl($folder.ServerRelativeUrl).DeleteObject();
        $ClientContext.ExecuteQuery();
        Write-Host "Folder has been deleted!"
    }
    else
    {
        Write-Host "Folder has not been deleted!"
    }
}

enter image description here

| improve this answer | |
  • Thanks, everyone. Your suggestions pointed me in the right direction. – Tomcat1208 Jan 17 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.