I have the following powershell:

$list = $ctx.Web.Lists.GetByTitle("Documents")
## View XML
$qCommand = @"
        <Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>Folder</Value></Eq></Where>
        <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
    <RowLimit Paged="TRUE">5000</RowLimit>
## Page Position
$position = $null

## All Items
$allItems = @()
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ListItemCollectionPosition = $position
    $camlQuery.ViewXml = $qCommand
## Executing the query
    $currentCollection = $list.GetItems($camlQuery)

## Getting the position of the previous page
    $position = $currentCollection.ListItemCollectionPosition

# Adding current collection to the allItems collection
    $allItems += $currentCollection
# the position of the last page will be Null
Until($position -eq $null)
$results = ""
foreach($item in $allItems){
write-host $item["Name"]
$item.Name | Out-File "C:\temp\test.csv" -append


Which should output the name of each folder, however it is just a blank line. I have tested the script with outputting the ID and that matches with the folders that are in the library it is just an issue when i try outputting the Name. Any ideas where i am going wrong?


The correct field value to use is FileLeafRef for folder names

Modify your code as write-host $item["FileLeafRef"] should output the folder name


Change $item["Name"] to $item["Title"] or $item.Name (don't have have connection to SP atm to check so advise if from top of my head)

  • Folders don't have a title field? also i tried this and produces the same result – James Becks Nov 14 '18 at 13:58
  • I updated my answer try with $item.Name – Marek Sarad Nov 14 '18 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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