GitHub Icon Image
GitHub

Export all List and Libraries with Item count and Permission in CSV

Summary

Get all lists and Libraries along with total Item count and permissions and export it in CSV file using below power shell script.

PnP Powershell result


CLI version of the script works the same but does not retrieve permissions information from the list

PnP Powershell result

  • PnP PowerShell
  • CLI for Microsoft 365

# Make sure necessary modules are installed
# PnP PowerShell to get access to M365 tenent

Install-Module PnP.PowerShell
$siteURL = "https://tenent.sharepoint.com/sites/Dataverse"
$ReportOutput="C:\SiteInventory.csv"
$ResultData = @()
$UniquePermission = "";
#  -UseWebLogin used for 2 factor Auth.  You can remove if you don't have MFA turned on
Connect-PnPOnline -Url  $siteUrl
 # get all lists from given SharePoint Site collection
 $lists =  Get-PnPList -Includes HasUniqueRoleAssignments,RoleAssignments
 If($lists.Count -gt 0){
   foreach($list in $lists){
    $members = "";
    if($list.HasUniqueRoleAssignments -eq $false){
        $UniquePermission = "Inherited"
    }
    if($list.HasUniqueRoleAssignments -eq $true){
        $UniquePermission = "Unique"    
    }
    if($list.RoleAssignments.Count -gt 0){
        foreach($roleAssignment in $list.RoleAssignments){
            $property = Get-PnPProperty -ClientObject $roleAssignment -Property Member
            $members += $property.Title + ";"
        }
    }
     $ResultData+= New-Object PSObject -Property @{
            'List-Library Name' = $list.Title;
            'Id'=$list.Id;
            'Parent Web URL'=$list.ParentWebUrl;
            'Item Count' = $list.ItemCount;
            'Last Modified' = $list.LastItemUserModifiedDate.ToString();
            'Created'=$list.Created;
            'Default View URL'=$list.DefaultViewUrl;
            'Permision'=$UniquePermission;
            'Members'=$members;
            'isHidden'=$list.Hidden;
        }
   }
 }

 $ResultData | Export-Csv $ReportOutput -NoTypeInformation

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


$siteURL = "https://tenent.sharepoint.com/sites/Dataverse"
$ReportOutput = "C:\SiteInventory.csv"
$ResultData = @()

$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login

}

# get all lists from given SharePoint Site collection
$lists = m365 spo list list --webUrl $siteURL
$lists = $lists | ConvertFrom-Json
If ($lists.Count -gt 0) {
    foreach ($list in $lists) {

        $listName = $list.Title
        $views = m365 spo list view list --webUrl $siteURL --listTitle $listName
        $views = $views | ConvertFrom-Json
        $defaultView = $views | Where-Object {$_.DefaultView -eq "True"}

        $ResultData += New-Object PSObject -Property @{
            'List-Library Name' = $listName;
            'Id'                = $list.Id;
            'Parent Web URL'    = $list.ParentWebUrl;
            'Item Count'        = $list.ItemCount;
            'Last Modified'     = $list.LastItemUserModifiedDate.ToString();
            'Created'           = $list.Created;
            'Default View URL'  = $defaultView.ServerRelativeUrl;
            'isHidden'          = $list.Hidden;
        }
    }
}

$ResultData | Export-Csv $ReportOutput -NoTypeInformation

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
Dipen Shah
Adam Wójcik
Alex Talarico

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI