foreach loop randomizes sort-object list

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
geryon
Posts: 1
Joined: Wed Jun 26, 2019 4:20 pm

foreach loop randomizes sort-object list

Post by geryon » Wed Jun 26, 2019 9:25 am

I have a Script that goes like this:

$var = Get-ChildItem C:\Temp\ -Name "_*.txt" | Sort-Object -Property Name

foreach ($item in $var){
do stuff
}

The Problem with this is, the script doesnt start at the top of the Array.
It is nessesary for it to start at the top, because it Imports files and has to get the latest last.

Can anyone help me solve this Problem please?

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: foreach loop randomizes sort-object list

Post by jvierra » Wed Jun 26, 2019 9:33 am

The code does start at the top of the sorted objects. The files are sorted by name. Sort by date to get the latest last.

Locked