SharePoint Online: PowerShell to a Get List
Requirement: Get a list in SharePoint Online using PowerShell
PowerShell to Get a SharePoint Online List
If you are looking for a way to get a list in SharePoint Online using PowerShell, this blog post will show you how to do just that. Let’s get a list in SharePoint Online and count items in a list using PowerShell:
#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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName="Project Tasks"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#get list sharepoint online powershell
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
Write-host "Total Number of List Items:" $List.ItemCount
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
SharePoint Online Get List using PowerShell
Similarly, you can get all lists from a SharePoint Online site as:
#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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get All Lists from the Web
$Lists =$Ctx.Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Iterate through each list
ForEach($List in $Lists)
{
#get all list namses in sharepoint online site using powershell
Write-host $List.Title
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
PnP PowerShell to Get a List in SharePoint Online
Here is how to get a list in SharePoint Online using PowerShell:
#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Documents"
#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#powershell get sharepoint online list
$List = Get-PnPList -Identity $ListName
Write-host $List.ItemCount
To get all lists using PnP PowerShell, use:
#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Get all sharepoint online lists
$Lists = Get-PnPList
#Get the Title of All Lists
$Lists | Select Title