Foreach Loop in PowerShell Not in Order

Question:

I am working with a config file for PowerShell and every time I call a foreach loop to grab the data it grabs it in the wrong order. See below:

config.ini

The foreach loop:

I get the following output:

I am assuming this has something to do with asynchronous processing from PowerShell, but what would be the best way to handle the queries from the config.ini file in the order I store them in there?

Note: I added the number to the end of the query (query01) just for testing purposes. The queries will not have them in my final config.ini.

EDIT:

Get-IniContent function:

Answer:

You need to change both hashtable declarations to ordered dictionaries. If you only change

to

your $ini is now an ordered dictionay but the nested hashtable created at

is still an unordered hashtable. You would need to change both of them to ordered dictionaries.

EDIT

There is also a ConvertTo-OrderedDictionary script on the Script Center that allows you to convert hashtables and arrays to ordered dictionaries if you don’t want to rewrite your function.

Source:

Foreach Loop in PowerShell Not in Order by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply