The information in this article was written against the second Community Technology Preview (CTP2) of Windows PowerShell 2.0. This information is subject to change in future releases of Windows PowerShell 2.0.
You know, sometimes the most important – and most useful – things in life are the little things.
And no, the Scripting Guys aren’t saying that just because we’ve spent a good part of our lives denying that we’re actually hobbits; we really believe that sometimes the most important – and most useful – things in life are the little things.
And no, we are not hobbits.
Take the second CTP release of Windows PowerShell, for example. No doubt most of the excitement surround this release will revolve around the new approach to working with remote computers; the ability to perform tasks as transactions; the addition to eventing; and enhancements to the Graphical PowerShell script editor. Those things are all worthy additions to Windows PowerShell. But equally worthy, at least in our view, is something you’re likely to overlook: a minor change to the Import-CSV cmdlet.
No doubt most of you are familiar with Import-CSV; as the name implies, this cmdlet makes it easy for you to import a CSV (commas-separated values) file, and to work with the individual fields in that file as…well…individual fields. And that’s great … as long as you have a comma-separated values file. But what if you’re using a character other than the comma as your delimiter? For example, what if you have a semicolon-delimited file or a tab-delimited file? Well, in that case, you’re out of luck; the Import-CSV cmdlet is for commas only.
Or at least Import-CSV used to be for commas only. In the latest CTP release, however, the Windows PowerShell team has added a new parameter – -delimiter – to Import-CSV. Big deal, you say? Who cares about one silly little parameter, you scoff? Let’s put it this way: we think you’ll change your tune once you see what –delimiter can do.
Let’s start by taking a peek at a text file that uses a semicolon as the delimiter:
Field1;Field2;Field3;Field4;Field5 a;b;c;d;e f;g;h;i;j k;l;m;n;o p;q;r;s;t u;v;w;x;y
Let’s say you haven’t upgraded to the most recent release of Windows PowerShell, and let’s say you’re interested only in the data found in field 2 (Field2) of this file. Let’s further say that you try using a command similar to this one to retrieve the data from field 2:
Import-CSV C:\Scripts\Test.txt | Select-Object Field2
What do you think you’ll get back when you run this command? Hopefully you answered: not much of anything. Here’s what the preceding command returns:
Field2 ------
Why didn’t we get anything back? That’s easy: because the old Import-CSV knows how to parse only a comma-separated values file. There aren’t any commas in our sample file, which means that Import-CSV has no idea how to read that file and then parse the data into individual fields.
But that was the old Import-CSV. With the new Import-CSV we can tell the cmdlet that we’re using the semicolon as the field delimiter; in fact, that’s exactly what we do with this command:
Import-CSV C:\Scripts\Test.txt -delimiter ";" | Select-Object Field2
As you can see, we simply tacked on the –delimiter parameter followed by our field delimiter, enclosed in double quote marks: ";". What are we going to get back when we execute this command? We’re going to get back only the data found in field 2:
Field2 ---- b g l q v
We told you that was an important – and useful – addition to Windows PowerShell.
Admittedly, you probably won’t run into too many semicolon-delimited files; what you are likely to run into are tab-separated files:
Field1 Field2 Field3 Field4 Field5 a b c d e f g h i j k l m n o p q r s t u v w x y
And that’s a problem. After all, it’s easy enough to enclose a semicolon in double quote marks so you can use it with the –delimiter parameter. But how are you supposed to enclose a tab in double quote marks? (Hint: Typing a double quote mark, pressing the TAB key, and then typing another double quote mark won’t work.)
Relax; the PowerShell team has thought of everything. As it turns out, PowerShell has an “escape sequence” – `t – that represents the tab character. Even better, `t works just fine with Import-CSV:
Import-CSV C:\Scripts\Test.txt -delimiter `t | Select-Object Field2
If we run the preceding command we should get back the following:
Field2 ---- b g l q v
Very nice, even for a hobbit.
Um, not that we are hobbits, mind you. Because we’re not.