I wanted to share a quick hint about a cmdlet, ConvertFrom-StringData, I had the opportunity to put to use recently. ConvertFrom-StringData has been around since version 2.0 I believe but I wasn’t able to confirm that. What it basically does is take a string input and convert any key value pairs (“this = something”) it finds and converts them to a hashtable (i.e. object) that is familiar and consumable by everything PowerShell.
I spend a lot of my time at work integrating, consuming, and otherwise finagling something that doesn’t know about PowerShell and making it work with PowerShell. Until recently I had a lot of code that imported various sundry content with Get-Content then mashed it with various permutations of Select-String or regexp of some variety just to get an object that I could run with. That is the case no more.
For example a file (called “something.conf”) with the following content:
# This is a comment URL=http://example.com HOST=s.example.com USER=bob smith # More comments FILE=somewhere.txt
Could be imported into a PowerShell hashtable as follows:
ConvertFrom-StringData -StringData (Get-Content -Raw -Path .\something.conf)
Will get you the following:
Name Value ---- ----- HOST s.example.com FILE somewhere.txt USER bob smith URL http://example.com
Which is a nice pretty hashtable. It even was smart enough to exclude the lines with comments (“#”). There are a couple of caveats here. If the file you are ingesting uses a comment character other than “#” (say “;”) then you would still have to do some manipulation before ingesting that string with the cmdlet. However ConvertFrom-StringData has cut out and certainly streamlined some of my mashing code. It is always nice to remove regexp stuff with a simple one-liner cmdlet.
In the command above I made sure to use the -Raw switch (new with version 3.0) on Get-Content which just reads the content in as one big string instead of a line by line array. This is helpful otherwise you get an array of hashtables with a key value pair each instead of an single hashtable with all keys and values. It could still work for you without the -raw parameter. Just keep in mind if you try to index the resulting array of hashtables you could make some false assumptions as the comments will occupy “empty” indexes within the array. So in the example above index 0 and 4 would be empty and would not include the contents of “URL” and “FILE” as you might expect.
It goes to show that you should always be looking at the various cmdlets, new and old, that come with PowerShell. You may find something that you can use to your advantage that will save you some labor. Unfortunately the help for ConvertFrom-StringData does not provide a “Get-Content” example which seems like an obvious one to me. Most of the examples use here strings which seem much more contrived. Why would you put that data into a here string only to convert it to a hash table, why not just build a hash table to begin with? Maybe there is a common pattern out there I just haven’t run across yet.
Either way ConvertFrom-StringData has been a real productivity boost for me now that I understand a couple of its gotchas. I can now parse my office compatriots Linux data better, faster, and stronger then they can, ha!