Additional Thoughts on Hashtables
I recently starting looking at the subreddit for PowerShell, and I’ve seen a lot of people using hashtables for just about everything. I’ve even seen a few people using them over at PowerShell.com and in both cases the usage was dubious at best. Here are my thoughts on it, and why hashtables are usually not the right choice.
I’ve written about this before here, so I won’t go into the technical aspects of why hashtables may or may not be better than an array of objects. But one of the key things to keep in mind is hashtables cannot be piped into other cmdlet’s. This is huge. That means in order to pass on your objects or to use any other cmdlet you must convert your hashtable to an array of objects. In most use cases, why not simply begin with the array and save yourself the hassle?
Another technique I’ve seen is building a hashtable, only to splat it into an array of objects:
This is a simplification, but it really made me scratch my head. You’re creating a temporary hashtable and then simply adding into an array of objects. Why not bypass the extra step and add to your object?
Now, technically both techniques are the same thing. I’m simply putting a hashtable directly into the Property parameter instead of building the hashtable into a variable and then adding it to the object. But–and I’ll say it again and again–why create a variable you only use once? It makes no sense, and it complicates the script unnecessarily. Another interesting thing I’ve seen is people using hashtables in place of an array of objects, one poster was even doing hashtables inside hashtables! Now, if you have a need for a multi-dimensional array–remember those from the days before we had objects with multiple properties?!–then this kind of construct makes some sense. But for simple data gathering it’s a pure nightmare. Getting your data back out of a hashtable, in an object form so it’s usable is hard and because of the very construction of the hashtable you’ll lose data! If you are using the key value to hold data, say the name of a server then when you use the GetEnumerator() method to pull the data out in object form, it’s just the values that will be pulled not the key. Data lost. My typical advice is to simply not use hashtables until you really understand their strengths and weaknesses. Once you full wrap your head around them they are truly powerful and you can do some amazing things with them!
I Blame Me
So why the sudden rise? It’s probably not that sudden, I just happened to be going to a place where people were using it a lot. It’s also interesting to see problems come and go in batches on the forums. It’s just a nature cycle of things. But one of the issues is me. And the other bloggers who talk about PowerShell–you know, the good ones. For us, the progression of our articles is very sequential. When I look back at some of the early posts I made a couple of years ago I cringe a little bit. But for most of my visitors the articles are accessed randomly. You do a Google search looking to solve a problem and you find a blog post and copy the code. We all do it! But as a visitor you’re often coming to a blog that has been going for quite some time and that person may be using some very advanced techniques to solve a unique problem they encountered, and simply put, this might not be the best solution for your problem. That’s one of the reason’s I often will run through the sections of my script, to really thoroughly explain what’s happening and why I decided to solve it in that manner. This helps me get a deeper understanding too, since I can’t write it too well if I don’t know what I’m talking about. I can’t tell you how many times I’ve been writing an article about a script I had written and I end up re-writing whole sections of the script because as I tear it down I see the problems!
In the end, my best advice is to really understand hashtables before you use them, and my article comparing the two might help you a bit: Objects and Hashtables
What are your thoughts?