Tìm kiếm trang web

SharePoint Online: Xóa hàng loạt thư mục khỏi CSV bằng PowerShell


Yêu cầu: Xóa hàng loạt thư mục trong SharePoint Online.

Làm cách nào để xóa hàng loạt nhiều thư mục trong SharePoint Online?

Bạn đã bao giờ muốn xóa hàng loạt nhiều thư mục khỏi SharePoint Online để dọn dẹp trang web của mình hoặc xóa nội dung lỗi thời hoặc không mong muốn chưa? Nếu vậy, có lẽ bạn đã quen với thử thách khi chọn từng thư mục rồi xóa nó. Bài đăng trên blog này sẽ chỉ cho bạn cách xóa nhiều thư mục trong SharePoint Online một cách nhanh chóng.

Chúng tôi có thể sử dụng tệp CSV có tập lệnh PowerShell để xóa hàng loạt thư mục cùng với các thư mục con và tệp của chúng:


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
$CSVFilePath = "C:\Users\Thomas\Desktop\BulkDeleteFolders.csv"
  
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the data from CSV file
$CSVFile = Import-CSV $CSVFilePath | Sort-Object ParentFolderSiteRelativeURL -Descending
  
#Read CSV file and delete each folder
ForEach($Row in $CSVFile)
{
    Try {
        #Check if folder exists
        $Folder  = Get-PnPFolderItem -FolderSiteRelativeUrl $Row.ParentFolderSiteRelativeURL -ItemName $Row.FolderName #-ErrorAction SilentlyContinue
        If($Folder -ne $Null)
        {
            #Delete the folder        
            Remove-PnPFolder -Name $Row.FolderName -Folder $Row.ParentFolderSiteRelativeURL -Recycle -Force
            Write-host -f Green "Folder '$($Row.FolderName)' Deleted Successfully from $($Row.ParentFolderSiteRelativeURL)"
        }
        Else
        {
            Write-Host "Folder '$($Row.FolderName)' doesn't exists at $($Row.ParentFolderSiteRelativeURL)" -ForegroundColor Yellow
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

Tập lệnh này cho phép bạn xóa nhiều thư mục cùng một lúc, giúp việc này hiệu quả hơn. Đây có thể là một kỹ thuật hữu ích nếu bạn cần xóa nhanh nhiều thư mục khỏi trang web của mình. Đây là tệp CSV của tôi có danh sách tên thư mục và URL liên quan đến trang web của thư mục mẹ.

Bạn có thể tải xuống CSV để xóa các thư mục từ đây:

SharePoint Online: Không thể xóa thư mục có tệp?

Tập lệnh trên hoạt động tốt khi trang web của bạn không được đặt theo chính sách lưu giữ. Tuy nhiên, bạn phải xóa tất cả các tệp và thư mục con của thư mục này để tránh hiện tượng “Bạn phải xóa tất cả các mục trong thư mục này trước khi có thể xóa thư mục này”. thông báo lỗi.

SharePoint Online: Tập lệnh PowerShell để xóa hàng loạt thư mục khỏi tệp CSV

Tập lệnh PowerShell này xóa đệ quy từng thư mục và nội dung của nó (tệp, thư mục con) khỏi tệp CSV trong một trang web nhất định.


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$CSVFilePath = "C:\Users\Thomas\Desktop\BulkDeleteFolders.csv"

#Function to delete all Files and sub-folders from a Folder
Function Empty-PnPFolder($Folder)
{
    #Get the site relative path of the Folder
    If($Folder.Context.Web.ServerRelativeUrl -eq "/")
    {
        $FolderSiteRelativeURL = $Folder.ServerRelativeUrl
    }
    Else
    {        
        $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.Web.ServerRelativeURL,[string]::Empty)
    }

    #Delete all files in the Folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
    ForEach ($File in $Files)
    {
        #Delete File
        Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
        Write-Host -f Green ("`tDeleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)        
    }
 
    #Process all Sub-Folders
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
    Foreach($SubFolder in $SubFolders)
    {
        #Exclude "Forms" and Hidden folders
        If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
        {
            #Call the function recursively
            Empty-PnPFolder -Folder $SubFolder
 
            #Delete the folder
            Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force -Recycle
            Write-Host -f Green ("`tDeleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL)
        }
    }
}
  
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the data from CSV file
$CSVFile = Import-CSV $CSVFilePath | Sort-Object ParentFolderSiteRelativeURL -Descending
  
#Read CSV file and delete each folder
ForEach($Row in $CSVFile)
{
    Try {
        #Check if folder exists
        $Folder  = Get-PnPFolderItem -FolderSiteRelativeUrl $Row.ParentFolderSiteRelativeURL -ItemName $Row.FolderName #-ErrorAction SilentlyContinue
        If($Folder -ne $Null)
        {
            Write-Host -f Yellow "Deleting Folder '$($Row.FolderName)' from $($Row.ParentFolderSiteRelativeURL)..."
            #Empty the Folder
            Empty-PnPFolder $Folder

            #Delete the folder
            Remove-PnPFolder -Name $Row.FolderName -Folder $Row.ParentFolderSiteRelativeURL -Recycle -Force
            Write-host -f Green "`tFolder '$($Row.FolderName)' Deleted Successfully!"
        }
        Else
        {
            Write-Host "Folder '$($Row.FolderName)' doesn't exists at $($Row.ParentFolderSiteRelativeURL)" -ForegroundColor Yellow
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

Tập lệnh này trước tiên sẽ xóa tất cả các tệp và thư mục con, sau đó xóa thư mục trong SharePoint Online. Để xóa hàng loạt tệp khỏi tệp CSV, hãy sử dụng: SharePoint Online: Xóa hàng loạt tệp khỏi CSV bằng PowerShell

Các câu hỏi thường gặp:

Làm cách nào để xóa thư mục trong SharePoint trực tuyến?

Để xóa thư mục trong SharePoint Online, hãy chọn thư mục từ danh sách hoặc thư viện tài liệu và nhấp vào nút “xóa” từ thanh công cụ. Bạn cũng có thể nhấp chuột phải vào thư mục và chọn “Xóa” từ menu ngữ cảnh. PowerShell cũng giúp xóa các thư mục trong SharePoint Online.
Thông tin thêm: Xóa thư mục trong SharePoint Online PowerShell

Làm cách nào để xóa tệp trong SharePoint Online bằng PowerShell?

Sử dụng phương thức $File.DeleteObject() trong CSOM hoặc lệnh ghép ngắn Remove-PnPFile trong PnP PowerShell để xóa tệp khỏi SharePoint Online.
Thông tin thêm: SharePoint Online PowerShell để xóa tệp

Bài viết liên quan