简体   繁体   中英

Access Sharepoint Online Recycle Bin through Powershell

I need to restore a massive amount of deleted files back into Sharepoint. Sharepoint is a disgusting beast that should be avoided at all costs, of course, but a client is running it and I need to find a resolution for them.

Connect-SPOService -Url https://clientdomain-admin.sharepoint.com -Credential $userCredential

So far so good.

$Site = Get-SPOSite
Write-Host $Site

Output: Microsoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSiteMicrosoft.Online.Sharepoint.Powershell.SPOSite

I am not kidding. So trying a different way:

$SiteUrl = "https://clientdomain.sharepoint.com"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Site = $Context.Site
$RecycleBinItems = $Site.RecycleBin
Write-Host "Total Number of Items:" $RecycleBinItems.Count

Output: "Total Number of Items: 0

I am stuck at this point. Does anyone know how to access the Recycle Bin this way?

Wow, been messing with this for hours and finally got it. Full code:

$SiteUrl = "https://clientdomain.sharepoint.com"
$AdminUserName = "administrator@clientdomain.com"
$Password = Read-host -assecurestring "Enter Password for $AdminUserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUserName,$Password)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
$Site = $Context.Site
$RecycleBinItems = $Site.RecycleBin
$Context.Load($Site)
$Context.Load($RecycleBinItems)
$Context.ExecuteQuery()
Write-Host "Total Number of Items:" $RecycleBinItems.Count

Output: 14440

Bisclavret's answer worked for me.

I needed to export the list so I added an export path before the query and then "$RecycleBinItems | Export-Csv -Path $FilePath NoTypeInformation" at the end as below.

$FilePath = "C:\Users\Admin\Documents\Restore from recycle bin\restore.csv" 
$Context.ExecuteQuery() 
$RecycleBinItems | Export-Csv -Path $FilePath 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM