51CTO Blog地址:https://blog.51cto.com/u_13969817
通常删除文件夹可以在UI界面操作比较方面,但是如果想快速的删除文件夹以及其child folder,该如何操作呢?
解决方案:本文将介绍如何使用PnP PowerShell脚本删除SharePoint文档库中的文件夹:第一步是通过运行cmdlet: Connect PnPOnline来完成连接到您的SharePoint Online网站;连接后,可以通过运行命令删除文件夹:Remove PnPFolder。
首先,将要删除的文件夹和子文件夹保存到csv文件中,如下所示:
执行PnP Powershell,删除上述文件夹和子文件夹,脚本如下所示:
$SiteURL = "https://byrondemo.sharepoint.com/sites/Project"
$CSVFilePath = "C:\Users\Byron\Desktop\BulkDeleteFolders.csv"
Connect-PnPOnline -Url $SiteURL -Interactive
$CSVFile = Import-CSV $CSVFilePath | Sort-Object ParentFolderSiteRelativeURL -Descending
ForEach($Row in $CSVFile)
{
Try {
$Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $Row.ParentFolderSiteRelativeURL -ItemName $Row.FolderName
If($Folder -ne $Null)
{
Write-Host -f Yellow "Deleting Folder '$($Row.FolderName)' from $($Row.ParentFolderSiteRelativeURL)..."
Empty-PnPFolder $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
}
}
如果预将文件和文件夹全部删除,那么命令为:
$SiteURL = "https://byrondemo.sharepoint.com/sites/Project"
$CSVFilePath = "C:\Users\Byron\Desktop\BulkDeleteFolders.csv"
Function Empty-PnPFolder($Folder)
{
If($Folder.Context.Web.ServerRelativeUrl -eq "/")
{
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl
}
Else
{
$FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($Folder.Context.Web.ServerRelativeURL,[string]::Empty)
}
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
ForEach ($File in $Files)
{
Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
Write-Host -f Green ("`tDeleted File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)
}
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
Foreach($SubFolder in $SubFolders)
{
If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
{
Empty-PnPFolder -Folder $SubFolder
Remove-PnPFolder -Name $SubFolder.Name -Folder $FolderSiteRelativeURL -Force -Recycle
Write-Host -f Green ("`tDeleted Folder: '{0}' at '{1}'" -f $SubFolder.Name, $SubFolder.ServerRelativeURL)
}
}
}
谢谢大家的阅读。
标签:Name,SubFolder,PnP,文件夹,File,Folder,365,Powershell,Row From: https://blog.51cto.com/u_13969817/9049107