Help with Sorting (Array)

Welcome Forums General PowerShell Q&A Help with Sorting (Array)

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 months, 2 weeks ago.

  • Author
    Posts
  • #128904

    Participant
    Topics: 6
    Replies: 13
    Points: 45
    Rank: Member

    Hi Folks, I'm looking for a some advice on how to get the array output to be sorted via newest Folder creation date

    I'm populating an array which will hold the list of IIS backup folders from a server.

    This is getting my all the folder names, but it's not in any meaningful order

    $backupFileList = @{}
    $backupfiles = Invoke-Command -ComputerName MYCOMPUTER { Get-ChildItem -Path "C:\Windows\System32\inetsrv\backup"} | select Name
    $backupfiles | foreach-object { $backupfileList[$_.Name] = $_.Name }
    

    What is the best way to get the data in $BackupFileList to be sorted with newest folder created first? (NB. It needs to be an array)
    I only need the name to show, as that is what I use to populate a list box in an GUI. But I need the list names to be sorted with newest created folder first!??!!

    I've tried to add the LastWriteTime with | select Name,LastWriteTime and then pipe that Sort-Object -Property LastWriteTime, but it never sorts 🙁
    I'd really appreciate any help to get this to sort correctly!
    Thanks a packet!
    Alessandro.

  • #128907

    Participant
    Topics: 1
    Replies: 1266
    Points: 1,391
    Helping Hand
    Rank: Community Hero
    Get-ChildItem -Path 'C:\Windows\System32\inetsrv\backup' -Directory | Sort-Object -Property LastWriteTime

    Of course I used another directory to test but it just works as intended. If you don't need the "LastWriteTime" anymore for your ListBox you can then use

      ... | Select-Object -Property Name

    and the array stays in this order.

    • #128910

      Participant
      Topics: 6
      Replies: 13
      Points: 45
      Rank: Member

      Hi Olaf, thanks for the reply. I did mess about with what you suggested, and I got it working via this

      $backupFileList = [ordered] @{}
      $backupfiles = Invoke-Command -ComputerName MYCOMPUTERNAME {Get-ChildItem -Path "C:\Windows\System32\inetsrv\backup" -Directory | Sort-Object -Descending -Property LastWriteTime | Select-Object -Property Name} 
      $backupfiles | foreach-object { $backupfileList[$_.Name] = $_.Name}

      I needed the -Descending to get latest files first, and I added the [ordered] for the hastable too.

      Now it populates the array in the correct order, and when I sent it back to Json it shows in correct order for the list box back to the GUI, so thanks again! 🙂

The topic ‘Help with Sorting (Array)’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort