Forum Discussion

Ganesh955's avatar
Ganesh955
Copper Contributor
Jul 02, 2023
Solved

How to loop through the Sub site lists and list items using PnP PowerShell

I can able to loop till the subsites, but I am not able to get the current(subsite )Lists/doc lib. the below code is working fine till to get subsite details.

 

$SiteColl = "https://SharePoint-Contose/sites/Test" 
Connect-PnPOnline -Url $SiteColl -UseWebLogin
$WebsCollection = Get-PnPSubWeb
#Iterate through each subsite
ForEach($Web in $WebsCollection)
{
Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen
}

 

from here I would like to get current web lists/lib please find the code below which I tried.

 

 ForEach($Web in $WebsCollection)
{
Write-host $Web.Url
$PnPListColls = Get-PnPList #("here it get the Root site lists/lib always so updated like below")
$PnPListColls = Get-PnPList -Web $Web
}
Here is the waring :WARNING: Parameter 'Web' is obsolete.

 

 

  • Hello Ganesh955,

    To loop through the lists and list items in the subsites using PnP PowerShell, you can try to modify your code like this:

    $SiteColl = "https://SharePoint-Contose/sites/Test"
    Connect-PnPOnline -Url $SiteColl -UseWebLogin
    $WebsCollection = Get-PnPSubWeb

    # Iterate through each subsite
    ForEach ($Web in $WebsCollection) {
    Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen

    # Connect to the current subsite
    Connect-PnPOnline -Url $Web.Url -UseWebLogin

    # Get the lists/libraries in the current subsite
    $PnPListColls = Get-PnPList

    # Loop through each list/library in the current subsite
    ForEach ($List in $PnPListColls) {
    Write-Host "List Title: " $List.Title

    # Get the items in the current list/library
    $ListItems = Get-PnPListItem -List $List.Title

    # Loop through each item in the current list/library
    ForEach ($Item in $ListItems) {
    Write-Host "Item Title: " $Item.FieldValues.Title
    }
    }
    }


    This should loop through the sub-sites, retrieve the lists/libraries, and then loop through the items in each list/library.

    Hope it helps.

    iIf you think my answer helped you, you can click on Mark as best response.

    Kindest regards

    Leon Pavesic

1 Reply

  • LeonPavesic's avatar
    LeonPavesic
    Silver Contributor

    Hello Ganesh955,

    To loop through the lists and list items in the subsites using PnP PowerShell, you can try to modify your code like this:

    $SiteColl = "https://SharePoint-Contose/sites/Test"
    Connect-PnPOnline -Url $SiteColl -UseWebLogin
    $WebsCollection = Get-PnPSubWeb

    # Iterate through each subsite
    ForEach ($Web in $WebsCollection) {
    Write-Host "Working On: " $Web.Url -BackgroundColor DarkGreen

    # Connect to the current subsite
    Connect-PnPOnline -Url $Web.Url -UseWebLogin

    # Get the lists/libraries in the current subsite
    $PnPListColls = Get-PnPList

    # Loop through each list/library in the current subsite
    ForEach ($List in $PnPListColls) {
    Write-Host "List Title: " $List.Title

    # Get the items in the current list/library
    $ListItems = Get-PnPListItem -List $List.Title

    # Loop through each item in the current list/library
    ForEach ($Item in $ListItems) {
    Write-Host "Item Title: " $Item.FieldValues.Title
    }
    }
    }


    This should loop through the sub-sites, retrieve the lists/libraries, and then loop through the items in each list/library.

    Hope it helps.

    iIf you think my answer helped you, you can click on Mark as best response.

    Kindest regards

    Leon Pavesic

Resources