0

I have a SharePoint online site collection with 17 subsites and a lot of document libraries, folders and sub folders in each subsite. Is there a powershell script that will list all the subsites, document libraries, folders and sub folders with their url for me?

1
  • Did the script work for you? Aug 29, 2023 at 12:53

1 Answer 1

1

Wrote this real quick using PnP PowerShell, the scripts connect to your site collection and then get's all the libraries and it's folders and store the results in a array. You can then export the array to a CSV using the cmdlt Export-CSV or pipe it to Out-GridView.

function Get-LibraryAndFolders {

    # get all libraries
    $lists = Get-PnPList | Where-Object {$_.BaseType -eq 'DocumentLibrary'}

    # loop through all the libraries
    foreach($list in $lists) {

        # add the library name, type and Url to the array
        $arr.add(
            [pscustomobject]@{
                "Name" = $list.Title
                "Type" = "Library"
                "Url" = $list.RootFolder.ServerRelativeUrl
            }
        )

        # get all the folders in the current list
        $folders = Get-PnPFolder -List $list

        # loop through all the folders
        foreach($folder in $folders) {

            # add the folder name, type and Url to the array
            $arr.add(
                [pscustomobject]@{
                    "Name" = $folder.Name
                    "Type" = "Folder"
                    "Url" = $folder.ServerRelativeUrl
                }
            )
        }
    }
}

# connect to rootsite
Connect-PnPOnline -Url "UrlToYourSite" -UseWebLogin

# create a array to store all objects in
$arr = [System.Collections.Generic.List[PSObject]]::new()

# get subsites
$subsites = Get-PnPSubWebs

# get library and folders
Get-LibraryAndFolders

# loop through all subsites
foreach($subsite in $subsites) {

    # connect to subsite
    Connect-PnPOnline -Url $subsite.Url -UseWebLogin

    # get library and folders
    Get-LibraryAndFolders

    # disconnect from subsite
    Disconnect-PnPOnline
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.