Weekend Scripter: An Insider’s Guide to PowerShell Arrays
Summary: Microsoft Scripting Guy, Ed Wilson, provides an insider’s guide to Windows PowerShell arrays. Microsoft Scripting Guy, Ed Wilson, is here. It is a quiet weekend in Charlotte, North Carolina in the United States. I am about to get back into the swing of things following nearly two weeks on the road for our Florida Windows PowerShell Road Show. The Scripting Wife has made herself scarce the past couple of days, and I am certain she is also trying to get back into a routine. I do not have any more public appearances, other than the Charlotte Windows PowerShell User Group meeting on July 5, 2012. We will be doing another “mini Scripting Games” type of meeting. The last one was WAY COOL! If you are in the area, you should definitely check it out. Even if you have to drive a bit, it will be worth the trip. Today, I thought I would go through the Hey, Scripting Guy! Blog archive, and list and review Hey, Scripting Guy! blogs that are related to arrays. Windows PowerShell makes working with arrays much easier than the methodology used from other languages. As a result, arrays often receive little attention. I tried to close this gap with a few blogs. In this resource guide, I detail these blogs.
Background of arrays
Using PowerShell Get-Member to Explore the .NET Framework OK. So this blog does not have the word array in the title. But it provides excellent background information about the System.Array .NET Framework class. Windows PowerShell arrays come from the System.Array .NET Framework class, and it is one reason I chose this class to illustrate working with Get-Member. Learn Simple Ways to Handle Windows PowerShell Arrays We begin with the question, “What is an Array?” then progress to discussing elements, indexes, and values. It describes the concepts of array boundaries, and examines the Count and the Length properties. The blog also discusses using the for statement and the Foreach-Object cmdlet to access members of an array. This is the first in a series of foundational blogs about arrays. Add, Modify, Verify, and Sort your PowerShell Array This continues the previous blog and dives into working with array elements, changing the values of elements, and adding to a previously existing array. I talk about searching an array for specific values and about two ways of sorting an array. This is a great blog, and is the second foundational post about arrays. Find the Index Number of a Value in a PowerShell Array In part three of the foundational posts about arrays, I talk about using the for statement to find the index number of a value in an array. Following that discussion, I talk about using the IndexOf static method from the System.Array .NET Framework class. This class was discussed in Using PowerShell Get-Member to Explore the .NET Framework, which is mentioned at the top of this section. Finally, I discuss working with only one-half of the array. This is a great blog, and you should definitely spend time mastering the techniques mentioned here. Easily Create and Manipulate an Array of Arrays in PowerShell This is part five in the multiple part array foundations series. In this blog, I first discuss creating an array of arrays. Next, I talk about how to access specific elements in an array of arrays. Using an array of arrays is a powerful technique—a bit advanced, but powerful nonetheless. This is a great introduction to that topic.
Speed Up Array Comparisons in PowerShell with a Runtime RegEx An excellent blog written by Scripting Guys Forum moderator and guest blogger, Rob Campbell. This blog compares using the Contains and the NotContains operators with the Match operator and a regular expression pattern. He uses the Measure-Command cmdlet to compare the time the two operations use. This is a special application, but it is a great trick to have in your tool pouch. Format Multilevel Arrays in PowerShell In this blog, I discuss using Windows PowerShell to work with multilevel arrays. I talk about creating arrays, adding additional elements to arrays, and creating arrays of other arrays. This is a really cool and foundational blog. Read a CSV File and Build Distinguished Names on the Fly by Using PowerShell The fourth blog in the Windows PowerShell arrays foundations series begins by reading a CSV file by using the Import-CSV cmdlet. Next I pipe the CSV contents to the Foreach-Object cmdlet and build a distinguished name. It is a cool technique. Combine Arrays and Hash Tables in PowerShell for Fun and Profit I discuss creating an array that contains hash tables in the various elements. This is a great technique, and it is an interesting blog. Well, that is about it for Hey, Scripting Guy! blogs about Windows PowerShell arrays. Hopefully the information will prime you with ideas and help you utilize these powerful techniques in your Windows PowerShell scripts. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy