当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell删除SharePoint文档库中的所有文件

作者:精品下载站 日期:2024-12-14 15:01:41 浏览:12 分类:玩电脑

PowerShell删除SharePoint文档库中的所有文件


要求:使用 PowerShell 删除 SharePoint 文档库中的所有文件。

[玩转系统] PowerShell删除SharePoint文档库中的所有文件

如何删除SharePoint文档库中的所有文件?

要删除 SharePoint 文档库中的所有文件,请执行以下步骤:

  1. 在 SharePoint 中打开要从中删除所有文件的文档库。
  2. 单击最左侧列标题中的复选框以选择库中的所有文件。如果库中有多个页面的文件,您可能需要在每个页面上重复此步骤以选择所有文件。
  3. 单击页面顶部工具栏中的“删除”按钮。
  4. 单击确认提示上的“确定”,确认您要删除所有选定的文件。

这将从文档库中删除所有选定的文件和文件夹,并将它们发送到回收站。

SharePoint PowerShell 删除所有文档

如果您想清除 SharePoint 文档库中的所有文件,PowerShell 提供了一种简单的方法。本文将向您展示如何使用 PowerShell 删除 SharePoint 文档库中的所有文件。

要删除 SharePoint 库中的所有文档,请使用以下 PowerShell:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Delete all files in a Folder    
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
    #Delete All Files in the Folder
    Foreach ($File in @($Folder.Files))  
    {
        #Delete the file
        $File.Recycle() | Out-Null

        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
    }

    #Delete files in Sub-folders
    Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
    {
        #Call the function recursively
        Delete-AllFilesFromLibrary($SubFolder)
    }
}

#Get the Web and Library
$Web = Get-SPWeb "https://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")

#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder

请注意,此脚本将库中所有文件夹-子文件夹中的所有文件发送到回收站。如果要永久删除文件,请使用: $File.Delete() 方法而不是 $File.Recycle()。

PowerShell 从 SharePoint 库中删除所有项目

上面的脚本删除所有文件 - 不删除文件夹和子文件夹。如果您想删除所有文件和文件夹怎么办?


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Delete all files in a Folder    
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
    #Delete All Files in the Folder
    Foreach ($File in @($Folder.Files))  
    {
        #Delete the file
        $File.Delete() | Out-Null

        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
    }

    #Delete files in Sub-folders
    Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
    {
        #Call the function recursively
        Delete-AllFilesFromLibrary($SubFolder)
    }

    #Delete folders
    ForEach ($SubFolder in @($Folder.SubFolders))
    {
        #Exclude "Forms" and Hidden folders
        If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
        {
            #Delete the Sub-Folder
            $SubFolder.Delete() | Out-Null
            Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'"
        }
    }
}

#Get the Web and Library
$Web = Get-SPWeb "https://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")

#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder 

要删除SharePoint Online中的所有文件,请参阅:PowerShell批量删除SharePoint Online中的文件

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯