Posey's Tips & Tricks
Using Configuration Files to Control PowerShell Scripts, Part 1: The Basics
Recently, I have been hard at work, creating some really complex PowerShell scripts related to a few projects that I have been working on. One of the big lessons that I have learned through all of this is that if you can avoid it, it's best not to hard code anything that might potentially need to change. Instead, you can store those values in a JSON file and then have your script to read the configuration files and adapt according to the values stored within it. This approach involves some extra work up front, but it can go a long way toward making your scripts more dynamic and flexible. In this article series, I want to show you how I go about creating a configuration file and how I make that configuration file work with my PowerShell scripts.
The first thing that you need to know about creating a configuration file is that while you can manually create such a file, you don't have to. With a little bit of extra work, you can create a setup script that creates the configuration file for you.
So before I show you how this works, let me take a moment and tell you about the scripts that I have created for the purposes of this article series.
My initial idea for this series was to create a simple "Hello World" style script that read a configuration parameter or two from a configuration file and reacted accordingly. While that is what I ultimately ended up doing, it occurred to me that this article series would be far more useful if I took a bit of extra time and created a more extensive configuration file. That way, I could demonstrate several different ways in which configuration items could be applied.
So what I ended up doing was to create a setup script called Setup.ps1. That Setup script displays a GUI featuring several drop down menus (which are technically called list boxes), each of which allows you to set an individual configuration parameter. When you have made your selections, you can click the Save button and all of these parameters will be written to a configuration file. You can see the Setup tool in Figure 1.
As previously mentioned, my main script was intended to be a simple Hello World type of script. For those who might not be familiar with the concept of a Hello World script, it's based on the idea that the first computer program that most students are asked to write is a simple script that does nothing more than to display the words Hello World on the screen.
Like most Hello World programs, my PowerShell script will indeed display the words Hello World inside of a GUI window. However, I am going to use the configuration file to customize things a bit. If you look back at the figure above for example, you can see that my setup tool includes a Language drop down menu. This menu allows you to choose between English, Spanish, Italian, and French. The words Hello World will be displayed in your chosen language.
In addition to displaying the phrase Hello World, the script also displays todays date. The reason why I included the date was to demonstrate the idea that you can use a configuration file to determine the format in which the date will be displayed. In this case, you can choose from a long date format, MM-DD-YY, or just a month and year.
Additionally, the Setup script lets you specify the font size and color that will be used, the background color, and even the size of the window. All of these options exist for the sole purpose of demonstrating how using a configuration file allows you to tailor a script's behavior based on your needs or on the user's preferences.
So with that in mind, I want to show you what the configuration file looks like and what you can expect from the Hello World script.
The configuration file's contents will, of course, vary depending on the options that you have chosen. However, you can get a feel for what the configuration file looks like in Figure 2. Remember, PowerShell creates this file automatically when you click the Save button within the Setup utility.
As you can see in the figure, all of the various configuration options and the selected values are listed within the JSON file. You can see the Hello World script's output (based on this JSON file) in Figure 3.
As you can see, the Hello World script's output is being completely controlled by the JSON file. The JSON file is controlling the window size and background color, the font size and color, the date format and even the text language.
So now that I have explained my general approach, I want to show you how my two scripts work. I will discuss the Setup.ps1 script in Part 2 and the HelloWorld.ps1 script in Part 3.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.