0
votes

I need to get count of Sharepoint Online recycle bin. I'm a site collection administrator on site collection that I work.

I tried to run this script:

Import-Module Microsoft.Online.SharePoint.PowerShell
$User = "[email protected]"
$PWord = ConvertTo-SecureString -String "MyPass" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
$SiteUrl = "https://contoso.sharepoint.com/sites/contoso"
Connect-PnPOnline -Url $SiteUrl -Credentials $Credential
$Web = Get-PnPWeb 
$Web.Title
(Get-PnPRecycleBinItem).count

And I get this error message:

Get-PnPRecycleBinItem : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At line:1 char:2

  • (Get-PnPRecycleBinItem).count
  •  + CategoryInfo          : WriteError: (:) [Get-PnPRecycleBinItem], ServerUnauthorizedAccessException
     + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.RecycleBin.GetRecycleBinItems
    
    

How to solve this problem?

2

2 Answers

0
votes

Not completely sure, but I think you need to capture the returned connection object from Connect-PnPOnline and use that for the -Connection parameter of Get-PnPRecycleBinItem:

$conn = Connect-PnPOnline -Url $SiteUrl -Credentials $Credential
$Web = Get-PnPWeb 
$Web.Title
(Get-PnPRecycleBinItem -Connection $conn).Count
0
votes

Per my test, I could get the count of Sharepoint Online recycle bin with following powershell:

$SiteUrl="https://tenant.sharepoint.com/sites/Team1"
$UserName="[email protected]"
$cred = Get-Credential -UserName $UserName -Message "Please enter password for $UserName"
Connect-PnPOnline -Url $SiteUrl -Credentials $cred
$Web = Get-PnPWeb
(Get-PnPRecycleBinItem).count

The user accessing the site is the site collection administrator and global admin: enter image description here

Steps on how to install PnP Powershell:

https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps