Khaine775 Khaine775 - 3 months ago 8
PowerShell Question

Getting a list of items from document library using PowerShell in SharePoint

So if I have the URL for a Document Library, how can I get all the items (in this case, folders) from inside the library? I've tried a few different things, but I can't seem to get it.

This is what I've tried so far:

$web = Get-SPWeb($completeUrl)
$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity http://url.com
$website = $spSite.OpenWeb()
$listItem = $website.GetListItem($completeUrl)


And it fails with an
Exception calling "GetListItem" with "1" argument(s)...


I'm not reeaally sure where to go from here. What I'm interested in is getting all the folders I have in the document library, which I know is found at
$completeUrl + /DocumentLibrary
which would equal something like
http://url.com/../../DocumentLibrary
for example. But I'm apparently going at it wrong.

Any hints?

Answer

I do not know your SharePoint library construction, however, from my point of view, you are missing list selection.

$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity http://url.com
$website = $spSite.OpenWeb()
$list = $website.Lists["List_Name_Here"]
$items = $list.GetItems()