Foreach Loop in PowerShell Not in Order


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:


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.


Get-IniContent function:


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


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.


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.


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

Leave a Reply