Summary: Microsoft Scripting Guy, Ed Wilson, talks about Windows PowerShell and the automatic Foreach feature.
Microsoft Scripting Guy, Ed Wilson, is here. One of my favorite features in Windows PowerShell (since Windows PowerShell 3.0) is what I call the automatic Foreach. Basically, it means that I can access a particular property of a collection by direct access; and therefore, I avoid having to use a Foreach to enumerate through the collection.
Note This is Part 4 of a four-part series about looping.
- In Part 1, Basics of PowerShell Looping: Foreach, I looked at the Foreach statement and talked about the problem of collections, arrays, and accessing specific items.
- In Part 2, PowerShell Looping: Using the Foreach-Object Cmdlet, I talked about using the Foreach-Object cmdlet to work with individual items from a collection.
- In Part 3, PowerShell Looping: Using While, I talked about using While to loop through a collection.
Today, I conclude the series by talking about the automatic Foreach feature.
What’s the problem with Foreach?
Suppose I have a Windows PowerShell cmdlet that returns a collection of objects. I don’t want to look at everything from that collection of objects. In fact, all I want to do is to return a list of a single property. For example, I use the Get-Process cmdlet to return a collection of process objects, but I only want the name of each process.
In the old days (Windows PowerShell 2.0 and Windows PowerShell 1.0), I had to use some sort of operation with Foreach. Now, maybe I don’t want to do that. So what can I do? A lot of times beginners would pipe the results to the Format-List cmdlet and get a list of names as shown in the following image:
That output is OK, but I want only the name of the processes. So newbies then start trying to clean up the output, and it becomes a complicated mess. I know. I have seen it in real life.
Next up, after a little research, beginners decide to not use Format-List (or one of the other Format* cmdlets). Instead they use the Select-Object cmdlet. Here is the output:
The output seems to be better, but there is still one little issue. When I redirect the output to a text file (maybe I need to use the list later), I discover a problem. It is not a big problem, and I can clean up things, but still it is a problem. The output is shown here:
The solution in Windows PowerShell 1.0 and Windows PowerShell 2.0 was to use looping. In this case, I generally used the Foreach-Object cmdlet and selected the name. This technique is shown here:
The automatic Foreach
Piping the output to the Foreach-Object cmdlet is not horribly hard. In fact, I have done it so many times in my life, that it is nearly automatic. But it is a bit of typing. Here is the command again:
Get-Process | ForEach-Object { $_.name }
Beginning in Windows PowerShell 3.0, there is an automatic Foreach. I can directly access a specific property from a collection without using a Foreach type of looping statement. It is as easy as obtaining a specific property from a singleton as shown here:
(Get-Process).name
The command and output are shown here.
This type of technique is great when it comes to things like Get-ADComputer where I can query to return a collection of computer objects, and then immediately access the name of said computers, and feed it directly to another command (such as Invoke-Command). It opens new doors in the efficiency of automation.
That is all there is to using the automatic Foreach. This also concludes my four-part series about looping. Join me tomorrow when I will have a guest post by Teresa Wilson, aka Scripting Wife.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments