Solved

List all files in a Folder of a Document Library

Posted on 2013-12-26
2
7,069 Views
Last Modified: 2013-12-29
Hi,

I want to user Powershell to list all the files in a particular folder in a document library. I tried the below and it does not work. Can help?

cls
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$webUrl = "http://sharepoint.com.sg/sites/SiteA"
$listName = "Trademark Opposition"

$sc = New-Object Microsoft.SharePoint.SPSite($webURL)
$web = $sc.OpenWeb()
$list = $web.Lists[$listName]

#Below code don't work... need advice what is the correct way
$foldFirstLvl = $list.Folders["Happy-Tiger Vitamin Functional Drink 11289668 in class 32"]
foreach ($item in $foldFirstLvl) {
   Write-Host $item.Name;
}

Open in new window

0
Comment
Question by:NicksonKoh
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
Comment Utility
Hi,
the problem is that afaik there is no direct way to get a specific folder from the folder collection.
But on the web object there is the getfolder method - but you have to pass the complete URL path to it.
The following works on my dev environment (SP 2010 Enterprise):
cls
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$webUrl = "http://sharepoint.com.sg/sites/SiteA"
$listName = "Trademark Opposition"
$folderName = "Happy-Tiger Vitamin Functional Drink 11289668 in class 32"

$sc = New-Object Microsoft.SharePoint.SPSite($webURL)
$web = $sc.OpenWeb()
$list = $web.Lists[$listName]

#Get the folder reference based on the url AND use the Files property
$foldFirstLvl = $list.ParentWeb.GetFolder($web.Url + "/" + $list.RootFolder.Url + "/" + $folderName)
foreach ($item in $foldFirstLvl.Files) {
   Write-Host $item.Name
}

Open in new window


HTH
Rainer
0
 
LVL 17

Author Comment

by:NicksonKoh
Comment Utility
Thx Rainer! Code works well and I am able to pick it from here. : )
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Synchronize a new Active Directory domain with an existing Office 365 tenant
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now