Share via

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

Answer accepted by question author

Rich Matheisen 48,116 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 

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. 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

    Was this answer helpful?

    0 comments No comments

  2. Limitless Technology 40,101 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--

    Was this answer helpful?


Your answer

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