Sort-Object 'Top' parameter in powershell version 5

Lukas Piech 21 Reputation points
2021-09-21T13:44:37.287+00:00

Dear All,
I am having a problem with following command when working with powershell in version 5:

$Folder = "C:\Temp"
Get-ChildItem $folder | Sort-Object -Descending -Property LastWriteTime -Top 1 

Below error message that i am receiving:

A parameter cannot be found that matches parameter name 'Top'

Any hints how to overcome this issue?
Regards,
Lukas

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-09-21T14:10:38.743+00:00

    Use Select-Object on the result of the Sort-Object.

    $Folder = "C:\Temp"
    Get-ChildItem $folder | 
        Sort-Object -Descending -Property LastWriteTime |
            Select-Object -First 1 
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-09-21T18:58:43.2+00:00

    Hello @Lukas Piech

    Never used -Top parameter before. I always use "-First" to show the top results. Have you tried:

     $Folder = "C:\Temp"  
     Get-ChildItem $folder | Sort-Object -Descending -Property LastWriteTime -First 1   
    

    Hope this helps,
    Regards,

    --If the the reply is helpful, please Upvote and Accept as answer--


  2. Lukas Piech 21 Reputation points
    2021-09-22T06:30:57.17+00:00

    Thank you Both for replying!

    I've used RichMatheisen-8856 solution, it works just as i wanted!

    Regards,
    Lukas

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.