简体   繁体   中英

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?

This will print out all folder names of a document library:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue    
$web = Get-SPWeb 'http://urltoyourweb'
$list = $web.Lists['DocumentLibrary']

function ListFolders($folder) {
    Write-Host $folder.Name
    $folder.Subfolders | % { if( $_.item -ne $null) { ListFolders $_}}
}

ListFolders $list.RootFolder

Explanation:

ListFolders is a function, that calls itself, if a subfolder is not null. This is called a recursive function. Now, we supply it with the root folder of a SPList and then iterate over its subfolders collection ( % is short for For-Each )

If this is too much inline piping for your taste, you could always write the more readable version of it:

##Same as $folder.Subfolders | % { if( $_.item -ne $null) { ListFolders $_}}
foreach($sfolder in $folder.SubFolders)
{
    if ($sfolder.item -ne $null)
    {
        ListFolders $sfolder
    }
}

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()

So what I had to do was call $items at the end to get the results. I initially thought the $list.GetItems() was going to return the list of items.

So my script looks like this:

Add-PSSnapin Microsoft.SharePoint.PowerShell
$spDocumentLibrary = $completeUrl + "/DocumentLibrary"
$spSite = Get-SPSite -Identity https://url.com
$website = $spSite.OpenWeb()
$list = $website.Lists["List_Name_Here"]
$items = $list.GetItems()
$items | select -property Title,DisplayName,Url | `
Export-CSV -Path "\\DesiredLocation\file.csv" -NoTypeInformation

As you can see I piped my results to a CSV file. Just wanted to note the following things.

  1. For this to work for you change the generic https://url.com to the legit URL that you are trying to get the results from.
  2. Change the Export-CSV -Path "DesiredLocation" to an actual file path (ie c:\\users\\yourprofile\\desktop\\filename.csv
  3. Change the CSV FileName to something that makes sense to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM