Using a Tuple in PowerShell

Doctor Scripto

Dr Scripto

Summary: Learn how to use a Tuple in Windows PowerShell

Hey, Scripting Guy! I read your article from yesterday, so I know about Tuples. How do I actually use one?


Hello HD,

Microsoft Scripting Guy, Ed Wilson, is here. I just got back from working out with my trainer. I am not nearly as sore today, but then there is always tomorrow. Anyway, I decided to drink a protein shake and a glass of water now. I will have tea later with my mid-morning snack. I grabbed my Surface Pro 3, and I am quickly checking my email to

Note  For a good introduction to Tuples, read What’s a Tuple, and Why Would I Want to Use It?

Creating Tuples

A two element Tuple is called a 2-tuple, or a pair. The way I create it is to use the Create static method from the System.Tuple .NET Framework class, and specify two elements. Here is an example of creating a pair:

$P = [System.Tuple]::Create("Flintstone","Rubble")

To access the two elements, I use parameter replacement and the Format (-f) operator. I specify each element (beginning at 0) with a pair of curly brackets. On the other side of the format operator, I specify the tuple element (item number).

Note  For parameter replacement and the Format operator, the elements are zero-based. Therefore, the first element is {0}. On the other hand, when tuples are referenced by item number, they are one-based. Therefore, the first tuple element is Item1

To use the tuple, I use parameter replacement and the Format operator. Here is an example:

"Fred's last name is {0} and Barney's last name is {1}" -f

  $p.Item1, $p.Item2

The image that follows illustrates creating a 2-tuple (pair) and accessing each of the elements.

Image of command output

If I want to create a 3-tuple (a triple), it works exactly the same way as creating a 2-tuple. All I need to do is to add an additional element. I access the third element in the same way as I access the other elements. Here is the code:

$P = [System.Tuple]::Create("Flintstone","Rubble","Dino")

"Fred's last name is {0} and Barney's last name is {1}

Fred's dog is really a dinosaur named {2}" -f

  $p.Item1, $p.Item2, $p.Item3

Here is an example of running the script, and the output produced by the script:

Image of command output

I can create a tuple with more than eight elements by using the Rest property. I do this by creating a nested tuple in the eighth element. When I reference the elements stored in the Rest property, I do so with a double type of attribution: $T.Rest.Item1.Item3. My new tuple is stored within Item1 of the Rest property. Here is an example of how to do this:

$T = [System.Tuple]::Create("Betelgeuse","RW Cephei","KW Sagitarii","KY Cygni","V354 Cephei",

        "WOH G64","Mu Cephei",[System.Tuple]::Create("V838 Monocerotis","VV Cephei A",

        "VY Canis Majoris"))

"The smallest of the top ten stars is {0}.

The largest of the top ten stars is {1}" -f $T.Item1, $T.Rest.item1.Item3

The script and output from the script are shown here:

Image of command output

HD, that is all there is to using tuples with Windows PowerShell. Join me tomorrow when I will have a guest blogger, Windows PowerShell MVP, Joel Bennett, talk about tuples and their differences from other data structures.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Comments are closed. Login to edit/delete your existing comments