Skip to main content
added 7 characters in body
Source Link
JoannaW_MSFT
  • 6.6k
  • 1
  • 8
  • 10
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>"
$UserName="<user>@<tenant>.onmicrosoft.com"
$Password ="<password>"
$ListTemplate = "Survey"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        $listsToDelete = @()
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()
 
        #Get all lists from the web  
        foreach($List in $Lists)
        {
            #Get the List
            if($List.BaseType -eq $ListTemplate){
                $listsToDelete +=$Lists.GetByTitle($List.Title)
                Write-Host "List:" $List.Title " will be Deleted Successfully!" -ForegroundColor Green 
            }
        }
        foreach($listToDelete in $listsToDelete) {
            $listToDelete.DeleteObject() #permanently delete
            #$listToDelete.Recycle() #Delete the List - Send to Recycle bin
        }
        $Context.ExecuteQuery()
        Write-Host "The Survey lists are Deleted Successfully!" -ForegroundColor Green 
    }
 
    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL)
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials
 
        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs) 
        $Context.executeQuery()
  
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
  
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }
 
    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>"
$UserName="<user>@<tenant>.onmicrosoft.com"
$Password ="<password>"
$ListTemplate = "Survey"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        $listsToDelete = @()
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()
 
        #Get all lists from the web  
        foreach($List in $Lists)
        {
            #Get the List
            if($List.BaseType -eq $ListTemplate){
                $listsToDelete +=$Lists.GetByTitle($List.Title)
                Write-Host "List:" $List.Title " will be Deleted Successfully!" -ForegroundColor Green 
            }
        }
        foreach($listToDelete in $listsToDelete) {
            $listToDelete.DeleteObject()
        }
        $Context.ExecuteQuery()
        Write-Host "The Survey lists are Deleted Successfully!" -ForegroundColor Green 
    }
 
    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL)
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials
 
        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs) 
        $Context.executeQuery()
  
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
  
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }
 
    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>"
$UserName="<user>@<tenant>.onmicrosoft.com"
$Password ="<password>"
$ListTemplate = "Survey"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        $listsToDelete = @()
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()
 
        #Get all lists from the web  
        foreach($List in $Lists)
        {
            #Get the List
            if($List.BaseType -eq $ListTemplate){
                $listsToDelete +=$Lists.GetByTitle($List.Title)
                Write-Host "List:" $List.Title " will be Deleted!" -ForegroundColor Green 
            }
        }
        foreach($listToDelete in $listsToDelete) {
            $listToDelete.DeleteObject() #permanently delete
            #$listToDelete.Recycle() #Delete the List - Send to Recycle bin
        }
        $Context.ExecuteQuery()
        Write-Host "The Survey lists are Deleted Successfully!" -ForegroundColor Green 
    }
 
    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL)
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials
 
        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs) 
        $Context.executeQuery()
  
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
  
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }
 
    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Source Link
JoannaW_MSFT
  • 6.6k
  • 1
  • 8
  • 10

You can use the following cmdlets:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>"
$UserName="<user>@<tenant>.onmicrosoft.com"
$Password ="<password>"
$ListTemplate = "Survey"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        $listsToDelete = @()
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()
 
        #Get all lists from the web  
        foreach($List in $Lists)
        {
            #Get the List
            if($List.BaseType -eq $ListTemplate){
                $listsToDelete +=$Lists.GetByTitle($List.Title)
                Write-Host "List:" $List.Title " will be Deleted Successfully!" -ForegroundColor Green 
            }
        }
        foreach($listToDelete in $listsToDelete) {
            $listToDelete.DeleteObject()
        }
        $Context.ExecuteQuery()
        Write-Host "The Survey lists are Deleted Successfully!" -ForegroundColor Green 
    }
 
    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL)
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials
 
        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs) 
        $Context.executeQuery()
  
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
  
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }
 
    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

How to get all lists in a SPO site collection using PowerShell

How to delete a SPO list using PowerShell