Help with Sorting (Array)

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 :frowning:
I’d really appreciate any help to get this to sort correctly!
Thanks a packet!

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.

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! :slight_smile: