7 Oct
Well, just for a change, lets cut straight to the script (
5pm EST Sunday):
Basically, this is a set of regular expressions that can be run in the pipeline to convert delimited text from one delimiter to another. If the new delimiter needs quotes added to make it work correctly, the converter will do so (actually, it adds a marker character Þ around every field, and then removes them where they are not needed, and replaces them with quotes where they are — long story, and I welcome any improvements).
Just as an example, assume a column-based text file like this Hosts.txt file (notice that the columns are separated by two or more spaces, since there are spaces within the text columns):
You can convert it to CSV by using this function, and write it back out to file like this: Get-Content Hosts.txt | Convert-Delimiter " + " "," | Set-Content Hosts.csv. Now, since my original problem was all about importing the files I also went ahead and wrote a slightly more magical function called Import-Delimited:
This will allow you to directly import most delimited text files. But not only that, it can import text from the pipeline! So you can actually create objects out of any legacy application that outputs delimited text! Just for example, with our previous text file, we could do any of the following one-liners:
Any questions? Here’s the pair of functions as a single script download. 
It’s a regular expression meaning one or more spaces followed by a space (ie: two or more spaces).
The regular expressions for separators should be pretty simple, really. For instance, a tab-delimited text file would just be “`t” and a comma-delimited file is just a comma: “,” ... if you wanted to make sure to get rid of any spaces around the commas, you could use “ +, +” but we should mostly be taking care of that already.
Import-Delimited actually uses Import-Csv in the background, and requires a header row which has a name for each column: You can’t have a two separators in a row, or a trailing separator (although the scripts try to clean that up). Incidentally, this is a really good point: you must have the first row be the delimited headers, otherwise your first object will be treated as the header row.