powershellsharepointcaml

Find all files on SharePoint Online between two dates


I am trying to write a script in PowerShell that copies all my files from one site to another site. I am trying to keep costs down by only taking so many files at the same time. I figured I would do this with a date range on Created Date. But I cannot get it to use date to filter the list nor can it get it to sort the list by Created. Here is what I have tried:

Get-PnPListItem -List $ListName  -PageSize 500  | Where-object {$_.FieldValue.Created -GT [DateTime]'02/16/2015'} 
    
Get-PnPListItem -List $ListName -PageSize 500 | where-object {($_.FileSystemObjectType -eq "File" -and $_.Created -gt [DateTime]'02/16/2015')} 
    
Get-PnPListItem -List $ListName -PageSize 500 | where-object { $_.FileSystemObjectType -eq "File" -and $_['Created_x0020_Date'] -gt $Created } 
    
$ListItems = $ListItems | Sort $_.Created # also tried $_['Created_x0020_Date']

If I use a CAML query I always get the exceed the list view threshold error even with defined in the query.

Can anyone help me figure this out?

    $Query = 
    "<View>
    <RowLimit Paged = 'TRUE'>100
    <Query>
    <Where>
    <And>
    <Geq>
    <FieldRef Name='Created' />
    <Value Type='DateTime'>2015-01-01T12:00:00Z</Value>
    </Geq>
    <Leq>
    <FieldRef Name='Created' />
    <Value Type='DateTime'>2015-01-30T12:00:00Z</Value>
    </Leq>
    </And>
    </Where>
    </Query>    
    </RowLimit>
    </View>";

     Get-PnPListItem -List $ListName  -PageSize 500  -Query $Query


Solution

  • Fieldvalues is a dictionary. You can use Where-Object like this.

    Get-PnPListItem -List $ListName -PageSize 500 | Where-Object {
        $_.fieldvalues['Created'] -gt [DateTime]'01/01/2015' -and
        $_.fieldvalues['Created'] -lt [DateTime]'01/31/2015'
    }
    

    You can even omit the cast to datetime since the LHS is a datetime

    Get-PnPListItem -List $ListName -PageSize 500 | Where-Object {
        $_.fieldvalues['Created'] -ge '01/01/2015' -and
        $_.fieldvalues['Created'] -le '01/31/2015'
    }