Recycle-xSPOList.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 64559d40-c5de-4480-ba81-b19e2f0a820f
 
.AUTHOR Chendrayan Venkatesan
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS SharePointOnline CSOM PowerShell
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES Microsoft.SharePoint.Client.dll
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Remove SharePoint List or Document Library and moves to recycle bin
 
#>
 
param 
(
    # SharePoint Online Url
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [Uri]
    $Url,

    # SharePoint Online Admin Credential
    [Parameter(Mandatory)]
    [System.Management.Automation.CredentialAttribute()]
    [pscredential]
    $Credential,

    # SharePoint List or Document Library name
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [string]
    $Title
)
Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.SharePoint.Client.dll'
function Recyle-xSPOList
{
<#
.SYNOPSIS
    Delete the SharePoint List or Document library
.DESCRIPTION
    The list or document library will not be deleted permanently instead it will be removed and moved to recycle bin.
.EXAMPLE
    C:\PS> Recyle-xSPOList -Url https://chensoffice365.sharepoint.com -Credential "Chendrayan@Chensoffice365.onmicrosoft.com" -Title "Demo123"
    Removes the list Demo123 from the given site.
.EXAMPLE
    C:\PS> 'DemoHide12' , 'Demo123' | Recyle-xSPOList -Url https://chensoffice365.sharepoint.com -Credential "Chendrayan@Chensoffice365.onmicrosoft.com"
    Removes multiple list or document library from the same site
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    Please refer the MSDN documentation for more information about recycle method by executing "help Recycle-xSPOList -Online"
#>

    [CmdletBinding(ConfirmImpact="High",HelpUri="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.recycle.aspx")]
    param 
    (
        # SharePoint Online Url
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [Uri]
        $Url,

        # SharePoint Online Admin Credential
        [Parameter(Mandatory)]
        [System.Management.Automation.CredentialAttribute()]
        [pscredential]
        $Credential,

        # SharePoint List or Document Library name
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]
        $Title
    )

    process
    {
        try 
        {
            $SPOClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Url)
            $SPOClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Credential.UserName,$Credential.Password)
            $List = $SPOClientContext.Web.Lists.GetByTitle($Title)
            $SPOClientContext.Load($List)
            [void]$List.Recycle()
            $SPOClientContext.ExecuteQuery()
            $SPOClientContext.Dispose()
            Write-Information -MessageData ([string]::Concat($Title , " is now moved to recycle bin")) -InformationAction Continue

        }
        catch
        {
            $_.Exception.Message
        }
    }
}