Ini File IconThis morning Aaron Parker was wondering if Hash Tables could be used to work with ini files:

Aaron Parker | @stealthpuppy | @remkoweijnen @msh_dave need to see if this approach works for editing INI files

I thought it was a great idea because in Hash Tables you can use the . operator to get or set a Hash Table entry. But I wondered what to do with sections in ini files. Then I got the idea to use nested Hash Tables for that.

The result is two functions, one to read an ini file into a nested Hash Table and one function to write it back to an ini file.

Let’s look at the code (all code can be download with the link at the bottom of the page), starting with reading the ini file:

Let’s take an example ini file, the notorious win.ini:

And this is how to use it:

But remember we did not write anything to disk, our Hash Tables exist only in memory. So we need a function to write the ini file back to disk:

And finally we can save the ini file back to disk:

Download PowerShell Script