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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,376 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Limitless Technology 39,356 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