Managing INI Files with PowerShell

Posted on June 29, 2015 by Jeff Hicks in PowerShell with 0 Comments

Traditionally, INI files are used as configuration sources, where an INI file consists of one or more sections with key pair settings.

Here’s an example of an INI file:

[General]
Action = Start
Directory = c:\work
ID = 123ABC

[Application]
Name = foo.exe
Version = 1.0

[User]
Name = Jeff
Company = Globomantics

If your application uses an INI file, then there’s not much you can do except let it do its thing. But let’s say you have an INI file that you want to move to an object-based PowerShell world. In this article, I’ll show you how to use an INI file in the world of objects, and I expect the process will serve more as a learning tool than a practical one.

As I approached the problem, I thought about what I wanted the end result to look like. I knew I had to write an object to the pipeline. The key pair combinations already look like a hash table that can easily be turned into a custom object.

The section name, [General], is also a likely candidate for a property. Let’s make this the property name, and the setting object the value.

Making the [General] section name the property. (Image Credit: Jeff Hicks)

Making the [General] section name the property. (Image Credit: Jeff Hicks)

This makes it easy to reference individual settings.

Giving [General] a property lets us easily reference individual settings. (Image Credit: Jeff Hicks)

Giving [General] a property lets us easily reference individual settings. (Image Credit: Jeff Hicks)

Now that I had manually drafted and tested commands to turn an INI section into an object, I need to create a tool to automate this process. I know my command needs a Path parameter to get the INI file. To simplify, I decided to strip off any comment starting with a semi-colon, as well as any blank or empty lines.

In the following line of code, I’m using a regular expression pattern.

Next, I initialized an empty object and hash table.

I’ll be processing the INI file from top to bottom, so I need a ordered hash table to keep all of the settings in order.

Because this process walks through the file line-by-line, I need to know the section heading name by using another regular expression pattern.

Next, let’s use another pattern with the –Replace operator to strip off the [ and ] characters.

If the line was a data setting, I could split each line and add it to my hash table.

The challenge is now that I need to finish creating the previous object before going onto the next section.

Sponsored

Here’s my logic: If the line matches my pattern for a section heading, and the hash table contained something, then create the custom object.

I then reinitalized the hash table and saved the section name, since I am now processing the next object.

This process continues for each line in the INI file. However, when I reached the end of the last setting, there’s a new section heading to trigger anything. Now, I need to add code to create the final object property.

Here’s my complete function, which includes comment-based help.

Once loaded, I can create a custom object out of the INI file.

Creating a custom object out of an INI file. (Image Credit: Jeff Hicks)

Creating a custom object out of an INI file. (Image Credit: Jeff Hicks)

I now have an objectified version of my original INI file. If I want to persist it, I can export it:

Later, I can easily re-import it.

Importing the INI file. (Image Credit: Jeff Hicks)

Importing the INI file. (Image Credit: Jeff Hicks)

Even if you don’t have a need for this functionality, I hope you learned something about PowerShell syntax and scripting concepts.

Sponsored

Although I like the option to use the Export-Clixml cmdlet, you can only use the XML file in PowerShell. Next time, we’ll take this a step further and export the INI file to a more traditional XML format that you can use anywhere.

 

Sponsored

Tagged with ,