0

I am logging into the sharepoint webiste and getting folder contents as follows:

$connection = Connect-PnPOnline -Url $siteName -Credential $credential -ReturnConnection

$wholeContent = Get-PnPFolderItem -FolderSiteRelativeUrl $filePath -ItemType File -Connection $connection

$Library =  $wholeContent | where { $_.Name -match "^Amma\s{1}\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1,2}\.zip$" }

Write-Output $Library

The output has the following format

Name # Type # Items/Size # Last Modified

with a list of files from which I want get the most recent file. As you have noticed from the above snippet that I used the regular expression on the "Name" attribute by $_.Name. How do I address the attribute "Last Modified" if I have to get the most recent file based on the attribute "Last Modified" ?

1 Answer 1

0

Try the below code, I have added the sort object

$connection = Connect-PnPOnline -Url $siteName -Credential $credential -ReturnConnection

$wholeContent = Get-PnPFolderItem -FolderSiteRelativeUrl $filePath -ItemType File -Connection $connection

$Library = $wholeContent | where { $_.Name -match "^Amma\s{1}\d{1,2}.\d{1,2}.\d{1,2}.\d{1,2}.zip$" } | Sort-Object LastWriteTime -Descending | select -Last 1

Write-Output $Library

1
  • $Library = $wholeContent | where { $_.Name -match "^Amma\s{1}\d{1,2}.\d{1,2}.\d{1,2}.\d{1,2}.zip$" } | Sort-Object LastWriteTime -Descending | select -Last 1 is the one that worked for me
    – sajis997
    Jan 15, 2020 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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