ค้นหาเว็บไซต์

SharePoint Online: ลบโฟลเดอร์ว่างโดยใช้ PowerShell


ข้อกำหนด: ลบโฟลเดอร์ว่างในไลบรารีเอกสาร SharePoint Online โดยใช้ PowerShell

คุณกำลังประสบปัญหากับโฟลเดอร์ว่างจำนวนมากในไซต์ SharePoint Online ของคุณหรือไม่ และถ้าคุณเป็นเหมือนฉัน คุณอาจสงสัยว่ามีวิธีที่ง่ายกว่านี้หรือไม่ เพราะการระบุและลบข้อมูลโดยใช้เว็บเบราว์เซอร์โดยการนำทางไปยังแต่ละโฟลเดอร์จะยุ่งยาก แต่ PowerShell เสนอวิธีกำจัดพวกมันอย่างรวดเร็วและง่ายดาย ในโพสต์บนบล็อกนี้ เราจะดูวิธีการลบโฟลเดอร์ว่างใน SharePoint Online โดยใช้ PowerShell

จะค้นหาโฟลเดอร์ว่างใน SharePoint Online ได้อย่างไร

หากโฟลเดอร์ไม่มีไฟล์หรือโฟลเดอร์ย่อยใดๆ จะถือว่าว่างเปล่า ต่อไปนี้คือวิธีที่เราสามารถค้นหาโฟลเดอร์ว่างใน SharePoint Online จากส่วนติดต่อผู้ใช้บนเว็บโดยใช้ จำนวนรายการย่อย (จำนวนรายการในระดับรากของโฟลเดอร์ และไม่ใช่จำนวนรวมของรายการทั้งหมด ในโฟลเดอร์ย่อยทั้งหมด) และ จำนวนโฟลเดอร์ย่อย (จำนวนโฟลเดอร์ย่อยในระดับรากของโฟลเดอร์)

  1. นำทางไปยังไลบรารีเอกสาร >> คลิกที่ "แก้ไขมุมมองปัจจุบัน" จากเมนูแบบเลื่อนลงมุมมอง
  2. เลือกคอลัมน์ "จำนวนโฟลเดอร์ย่อย" และ "จำนวนรายการย่อย" และคลิกตกลง

  3. ขณะนี้ มุมมองจะแสดงจำนวนรายการและจำนวนโฟลเดอร์ในระดับบนสุดของแต่ละโฟลเดอร์

PowerShell เพื่อค้นหาโฟลเดอร์ว่างใน SharePoint Online:

สคริปต์นี้จะสแกนแต่ละโฟลเดอร์ในไลบรารีเอกสารที่กำหนดและตรวจสอบว่าจำนวนไฟล์และโฟลเดอร์ย่อยเท่ากับ '0' หรือไม่


#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"
  
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
  
#Get Credentials to connect
$Cred= Get-Credential
   
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
 
#Define the CAML Query to get folders
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <And>
                <And>
                    <Eq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Eq>
                    <Eq><FieldRef Name='ItemChildCount' /> <Value Type='Counter'>0</Value></Eq>
                </And>
                <Eq><FieldRef Name='FolderChildCount' /> <Value Type='Counter'>0</Value></Eq>                    
            </And>
        </Where>
    </Query>
</View>"
 
#Get All List Items matching the query
$Folders = $List.GetItems($Query)
$Ctx.Load($Folders)
$Ctx.ExecuteQuery()
 
Write-host "Total Number of Empty Folders:"$Folders.count
 
#Loop through each List Item
ForEach($Folder in $Folders)
{ 
    Write-host $Folder.FieldValues.FileRef
}

สคริปต์นี้แสดงรายการโฟลเดอร์ว่างทั้งหมดในทุกระดับของไลบรารีที่กำหนด อย่างไรก็ตาม เมื่อเราลบโฟลเดอร์ว่าง โฟลเดอร์หลักก็จะว่างเปล่าเช่นกัน (โดยที่โฟลเดอร์หลักมีโฟลเดอร์ว่างเพียงโฟลเดอร์เดียวและไม่มีไฟล์อื่นอยู่ในนั้น!)

SharePoint Online: PowerShell เพื่อลบโฟลเดอร์ว่าง

สคริปต์ PowerShell นี้จะลบโฟลเดอร์ว่างทั้งหมดออกจากไลบรารีเอกสาร SharePoint Online ที่กำหนด


#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"

Function Delete-SPOEmptyFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
    #Get All Sub-Folders from the given Folder
    $SubFolders = $Folder.Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()

    #Process Each Sub-Folder Recursively
    ForEach($SubFolder in $SubFolders)
    {
        #Exclude "Forms" and Hidden folders
        If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))) -and $SubFolder.Name -ne $RootFolder.Name)
        {
            #Call the function recursively
            Delete-SPOEmptyFolders $SubFolder
        }
    }

    #Get All Files and Sub-Folders from the Sub-folder
    $SubFolders = $Folder.Folders
    $Files = $Folder.Files
    $Ctx.Load($SubFolders)    
    $Ctx.Load($Files)
    $Ctx.ExecuteQuery()

    Write-host -f Yellow "Checking if the folder is Empty:" $Folder.serverRelativeURL
    #Delete Empty Folders
    If($SubFolders.Count -eq 0 -and $Files.Count -eq 0)
    {
        #Delete the folder
        $EmptyFolder=$Ctx.web.GetFolderByServerRelativeUrl($Folder.ServerRelativeUrl)
        $EmptyFolder.Recycle() | Out-Null
        $Ctx.ExecuteQuery()
        Write-host -f Green "`tDeleted Empty Folder:"$Folder.ServerRelativeUrl  
    }
}

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Migration Documents"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Root Folder of the Library
    $List = $Ctx.web.Lists.GetByTitle($LibraryName)
    $RootFolder = $List.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.ExecuteQuery() 

    #Call the function to delete empty folders from a document library
    Delete-SPOEmptyFolders $RootFolder
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell เพื่อลบโฟลเดอร์ว่างใน SharePoint Online:

มาทำให้สคริปต์ด้านบนง่ายขึ้นด้วย PnP PowerShell:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$DocumentLibraryName = "Documents"
 
#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Credentials (Get-Credential)

#Get the web & folder
$Web = Get-PnPWeb
$List = Get-PnPList -Identity $DocumentLibraryName -Includes RootFolder

Function Delete-PnPEmptyFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)
    #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
            Delete-PnPEmptyFolder -Folder $SubFolder
        }
    }
    #Get all files & Reload Sub-folders from the given Folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder

    If ($Files.Count -eq 0 -and $SubFolders.Count -eq 0)
    {
        #Delete the folder
        $ParentFolder = Get-PnPProperty -ClientObject $Folder -Property ParentFolder
        $ParentFolderURL = $ParentFolder.ServerRelativeUrl.Substring($Web.ServerRelativeUrl.Length+1)    
        Remove-PnPFolder -Name $Folder.Name -Folder $ParentFolderURL -Force -Recycle
        Write-Host -f Green ("Deleted Folder: '{0}' at '{1}'" -f $Folder.Name, $Folder.ServerRelativeURL)
    }

}
#Call the Function to Delete empty Folders
Delete-PnPEmptyFolder $List.RootFolder

สรุป

โดยสรุป การลบโฟลเดอร์ว่างใน SharePoint Online อาจเป็นงานที่ใช้เวลานาน แต่ด้วยการใช้ PowerShell จึงสามารถดำเนินการได้โดยอัตโนมัติ ด้วยการใช้สคริปต์ที่ให้ไว้ในบทความนี้ คุณสามารถเอาโฟลเดอร์ว่างใดๆ ในสภาพแวดล้อม SharePoint Online ของคุณออกได้อย่างรวดเร็วและง่ายดาย ทำให้ไซต์ของคุณจัดระเบียบและเป็นข้อมูลล่าสุด

บทความที่เกี่ยวข้อง