Use PowerShell to Loop Through Folders in a Library

Here is an example of using PowerShell to look through folders in a SharePoint Document Library.


if(-not(Get-PSSnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$site = Get-SPweb "http://sharepointed.com/site/taco/"

$mList=$site.Lists["Shared Tacos"]

foreach($f in $mlist.Folders)
 {
 Write-Host $f.Name
 }

$site.dispose()

2 thoughts on “Use PowerShell to Loop Through Folders in a Library

  1. I won’t be able to test this for a couple days, but the basic idea would be to do another loop inside the loop.

    foreach(folder in folders)
    {
    #then something like
    write-host folder.title
    if(folder.children.count > 0)
    {
    loop again…
    }
    }

    You can also look into recursion.

Leave a Reply

Your email address will not be published.