OneDrive for Business:使用 PowerShell 从回收站还原所有文件和文件夹
要求:使用 PowerShell 从 OneDrive for Business 还原所有已删除的文件和文件夹。
PowerShell 用于恢复 OneDrive for Business 中的所有已删除项目
OneDrive for Business 将所有已删除的项目存储在回收站中。如果您不小心删除了某个项目,您可以从回收站恢复该项目。另一篇文章中解释了如何通过 Web 浏览器界面从 OneDrive for Business 回收站恢复已删除的项目:如何从 OneDrive 恢复文件和文件夹?在这篇文章中,让我们看看如何使用 PowerShell 从回收站恢复所有已删除的文件和文件夹。
请注意,在继续还原之前,您必须拥有 OneDrive 网站的网站集管理员权限。
#Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount = "Salaudeen@Crescent.com" #UPN
Try {
#Connect to Admin Center
Connect-PnPOnline -Url $AdminCenterURL -Interactive
#Get onedrive site URL of the user
$OneDriveURL = Get-PnPUserProfileProperty -Account $UserAccount | Select -ExpandProperty PersonalUrl
If($OneDriveURL -ne $null)
{
#Connect to OneDrive site
Connect-PnPOnline -Url $OneDriveURL -Interactive
#Get All items in the recycle bin
$RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000
#Check if there are any deleted items in recycle bins
If($RecycleBinItems.count -eq 0) {
Write-host "No Items found in the recycle bin!" -f Yellow
Break
}
#Restore all items from the recycle bin
$RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000
ForEach($Item in $RecycleBinItems)
{
#Get the Original location of the deleted file or folder
$OriginalLocation = "/"+$Item.DirName+"/"+$Item.LeafName
If($Item.ItemType -eq "File")
{
$OriginalItem = Get-PnPFile -Url $OriginalLocation -AsListItem -ErrorAction SilentlyContinue
}
Else #Folder
{
$OriginalItem = Get-PnPFolder -Url $OriginalLocation -ErrorAction SilentlyContinue
}
#Check if the item exists in the original location
If($OriginalItem -eq $null)
{
#Restore the item
Restore-PnPRecycleBinItem -Identity $Item -Force
Write-host "Restored Item '$($Item.LeafName)' to '$($Item.DirName)'" -f Green
}
Else
{
Write-Host "A file or folder with this name $($Item.LeafName) already exists in '$($Item.DirName)', Skipping.." -f Yellow
}
}
}
Else
{
Write-host "OneDrive site for the user doesn't exist!" -f Yellow
}
}
Catch {
Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
相关文章:使用 PowerShell 从 SharePoint Online 中的回收站还原已删除的文件