I need to process files in order from oldest create date to newest.

I found the sort-object command which is what I need however I don't have a clue how to add it to my ForEach() statement.

Can someone show me how to do it?


Get-ChildItem -Path C:\Users\Tom\ -Filter "*.journal" | Sort-Object -Property CreationTime

ForEach ($sourcefile In $(Get-ChildItem $source | Where-Object { $_.Name -match "Daily_Reviews\[\d{1,12}-\d{1,12}\].journal" }))
    #### Process files in order from oldest to newest
    $file = $source+$sourcefile

You simply pipe the unsorted array to Sort-Object and iterate through the result like this:

(caret escapes and line breaks included for readability)

ForEach ($sourcefile In $(Get-ChildItem $source ^
           | Where-Object { $_.Name -match "Daily_Reviews\[\d{1,12}-\d{1,12}\].journal" } ^
           | Sort-Object -Property CreationTime))
    #### Process files in order from oldest to newest
    # Do-Whatever -With $sourcefile

Note that the first line with the Get-ChildItem call in your code does nothing but display a sorted list of **.journal* files - this output is not processed any further in your script.

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.