SharePoint Online: Get All Lists using PowerShell
Requirement: SharePoint Online Get All Lists inventory using PowerShell
Get All Lists in SharePoint Online using PowerShell CSOM:
In this blog post, we will take a look at how to use PowerShell to get a list of all the lists in a SharePoint Online site. This can be useful for finding specific lists or simply getting an overview of all the lists on the site. Let’s take a look at how it works!
Here is the PowerShell to get all lists in a SharePoint Online Site:
#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"
#Function to Get all/specific list from site
Function Get-SPOList()
{
Param
(
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Web] $Web,
[Parameter(Mandatory=$false)] [string] $ListName
)
#Get the Context
$Ctx = $Web.Context
#Get a single list or All Lists
If($ListName)
{
#sharepoint online get list powershell
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
Return $List
}
Else
{
#sharepoint online get all lists powershell
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
Return $Lists
}
}
#Parameters
$SiteURL="https://Crescent.sharepoint.com"
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#sharepoint online powershell get all lists
$Lists = Get-SPOList -Web $Ctx.Web
#Extract List data
$ListCollection = @()
ForEach($List in $Lists)
{
$ListData = New-Object -TypeName PSObject
$ListData | Add-Member -MemberType NoteProperty -Name "Title" -Value $List.Title
$ListData | Add-Member -MemberType NoteProperty -Name "Itemcount" -Value $List.Itemcount
$ListData | Add-Member -MemberType NoteProperty -Name "BaseTemplate" -Value $List.BaseTemplate
$ListData | Add-Member -MemberType NoteProperty -Name "Created" -Value $List.Created
$ListData | Add-Member -MemberType NoteProperty -Name "LastItemModifiedDate" -Value $List.LastItemModifiedDate
$ListCollection += $ListData
}
#Export List Inventory to CSV
$ListCollection | Export-csv -Path "C:\Temp\list-inventory.csv" -NoTypeInformation
This PowerShell gets either a single list or all lists from the SharePoint Online site based on the given parameters.
How to Get a List in SharePoint Online using PowerShell?
To get a list in SharePoint Online, use:
#Call the function to get a List
$List = Get-SPOList -web $ctx.Web -ListName "Documents"
Write-host "Total Number of Items in List:" $List.ItemCount
PnP PowerShell to Get All Lists from SharePoint Online Site:
Let’s get all lists from the current SharePoint Online site.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get all lists
$Lists = Get-PnPList
#Get List Title, Description and Number of Items
$Lists | Select Title, Description, ItemCount
Similarly, to get a specific list in SharePoint Online using PnP PowerShell, use:
#Connect to PnP Online
Connect-PnPOnline -Url "https://Crescent.sharepoint.com" -Credentials (Get-Credential)
#Get the List
$List = Get-PnPList -Identity "Documents"
#Get Number of Items in the List
Write-host "Total Number of List Items:"$List.ItemCount
Hi, when I run the get all lists powershell, it’s returning tenant.sharepoint.com sites and lists, but not anything in tenant.sharepoint.com/sites/sitename, which is what I really need.
Well, You have to iterate through each site in the tenant and get lists from them: SharePoint Online: PowerShell to Iterate through All Site Collections in the tenant