Office 365: How to get recycle bin items in a SPO site using PowerShell!

This time I’m sharing a PowerShell script allows to get all the items contained in the recycle bin of a SharePoint Online (SPO) site. The script defines a function that uses the Client Side Object Model (CSOM) to get all the recycle bin items in a SPO Site. Bear in mind that the information got from recycle bin is related to the user executing the script.  You can download the script from the following Url:How to get recycle bin items in a SharePoint Online Site

############################################################################################################################################

# Script that allows to get all the items in the recycle bin of a SPO Site.

# Required Parameters:

#  -> $sUserName: User Name to connect to the SharePoint Online Site.

#  -> $sPassword: Password for the user.

#  -> $sSiteUrl: SharePoint Online Site.

############################################################################################################################################

 

$host.Runspace.ThreadOptions = "ReuseThread"

 

#Definition of the function that gets all the items in the recycle bin of a SPO Site.

function Get-ItemsSPOSiteRecycleBin

{

    param ($sSiteUrl,$sUserName,$sPassword,$sCSOMPath)

    try

    {    

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

        Write-Host "Getting all items in the Recycle Bin of a SPO Site" -ForegroundColor Green

        Write-Host "----------------------------------------------------------------------------"  -ForegroundColor Green

     

        #Adding the Client OM Assemblies        

        $sCSOMRuntimePath=$sCSOMPath +  "\Microsoft.SharePoint.Client.Runtime.dll"        

        $sCSOMPath=$sCSOMPath +  "\Microsoft.SharePoint.Client.dll"

                     

        Add-Type -Path $sCSOMPath         

        Add-Type -Path $sCSOMRuntimePath        

 

        #SPO Client Object Model Context

        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 

        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)  

        $spoCtx.Credentials = $spoCredentials 

 

        #SPO Site

        $spoWeb = $spoCtx.Web

        #Recycle Bin Content for the user

        $spoRecycleBinItemCollection = $spoWeb.RecycleBin

        $spoCtx.Load($spoRecycleBinItemCollection)

        $spoCtx.ExecuteQuery()

        foreach($spoRecycleBinItem in $spoRecycleBinItemCollection){

            Write-Host "Item:" $spoRecycleBinItem.Title "- Deleted By:" $spoRecycleBinItem.DeletedByName "- Deleted Date:" $spoRecycleBinItem.DeletedDate

        }

        $spoCtx.Dispose()

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()   

    }    

}

 

#Required Parameters

$sSiteUrl = "https://<SPO_Site>" 

$sUserName = "<SPOUser>@<O365DOmain>.onmicrosoft.com" 

#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

$sPassword= ConvertTo-SecureString "<O365Password>" -AsPlainText -Force

$sCSOMPath="<CSOM_Path>"

 

Get-ItemsSPOSiteRecycleBin -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sCSOMPath $sCSOMPath

And bellow you can see the result you get once you execute the script in Visual Studio or PowerShell ISE:

image

Anuncios

Un pensamiento en “Office 365: How to get recycle bin items in a SPO site using PowerShell!

  1. Pingback: SharePoint & Office 365: Resumen de posts (XI)! | Pasión por la tecnología...

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s