foreach loop randomizes sort-object list

Ask your 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.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
geryon
Posts: 1
Last visit: Wed Jun 26, 2019 9:25 am

foreach loop randomizes sort-object list

Post by geryon »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: foreach loop randomizes sort-object list

Post by jvierra »

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.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked