In a C# video, I heard about the System.collections.stack collection (PowerShell Stack Collection). I immediatly thought that this could really be helpfull to me when I am confronted again to work with a big amout of objects that needs to be handled in a specific order. In this article i’ll cover the main mechanisms that you need to know to effeciently use the powershell stack collection.
 system.collections.stack
Luckly enough, anything that works in .Net (should) also work(s) in Powershell. So, let’s try it out 🙂
The System.Collection.Stack follows the principle of last in, first out.
A simple example of the powershell stack collection would be the following one: (Don’t worry to much about the syntax here, I cover the ‘push‘ method in detail a bit further down in this article).

If we check what the “$mystack” variable contains it will show us the following:
You’ll notice that the last object I added (the “district” one) is displayed at first. This is the whole purpose of the powershell stack collection. The powershell stack collection allows you to “stack” items (objects, etc..) one on top of each other.
 As opposed with the queue collection, wich returns the most old item in the collection (The one added first), the powershell Stack collection will return the last item you added to the collection (The most recent one). So, in other words The last item you added, will be the first one to be returned.

Use cases for the powershell system.collections.stack collection:

I haven’t really found a use case for this (yet) in my daily work, but, it worth to know it exists, and it could be usefull if we meet a use case once.
Daniel Meier comment this article on Facebook, explained how he has been using the Stack Collection:
“I’ve used stacks when changing directories. I’ll put the current directory on the stack then cd to a new directory, put it on the stack, etc. Then I can go back out of each directory to the previous one. I do this when walking a directory tree.”
Indeed, that is a perfect use case! Needing to go through (and back) a specific path (Directory, Registry, List, WebSite). It allows to use the exact same opposite path when moving back.
If you have used the powershell Stack collection before, please share with us for what use case you have used it (via the comment section below).
In the mean time, below I explain how the powershell stack collection works.

Methods and properties you don’t want to miss:

Lets have a look at the members of our collection object:

 

 

We will focus on the three most interesting ones of the stack array:
  • push
  • pop
  • peek

the .push() method

As you could have noticed before, in our stack example above, we already used the push() method, and not add() (which doesn’t exists on the stack object type).

As demonstrated earlier, the push() method allows us to add a new element onto our stack. yes onto our stack, not into the stack.

 

 

The Pop() method

The pop method will give us the possibility to retrieve the item on the top of our stack. This means, that the pop() method returns the last item that has been added.

powershell collection stack pop

As you can see, “district” was the last item that we added, but the first one to be returned when we called the pop() method.

 

The Peek() method:

The peek method will work exactly as the pop() method, except,  that the item that was returned will not be removed from the stack. As it’s name suggest, it allows you to peek onto the stack, and to see what would eventually  be returned if you would call the pop() method.

AS you can see in the example above, the peek method (in red) returns the item, but doesn’t removes it from the powershell stack collection.

Using the pop method, returns the “gulick” item just as the peek method informed us it would do, and in this case, removed it from the stack item.

A word about the ‘count’ property:

The stack collection instance comes with a ‘count‘ property (yes, a property, not a method!). It allows (as you might have guessed) to get the count of the number of items in your current powershell stack collection. This is a convenient property to check, to go through your stack collection as showcased in the following example:

 

The powershell Stack collection allows us to go through a collection, and return each item without using a loop such as a foreach or a for statement.

 

Conclusion:

This can be really handy since it allow you to go through collections of objects/ items without the need to iterate through them, or even to know how many items you currently have.
The second positive thing is that you can now ‘really’ have way of controlling the order in which each element will be treated. Since we know that the pop() method of the powershell stack collection returns the most young item from the collection (the last one added).
Have you used a stack collection already in one of your scripts? I’ll be curious to know how you used. Let us know!
That’s it for today
#Stéphane

Links

MSDN link to the Collections.Stack –> msdn

By | 2018-01-11T17:00:52+00:00 January 9th, 2018|PowerShell, Treasure chest|2 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

2 Comments

  1. Weblee July 14, 2018 at 1:15 am - Reply

    Without a stack pointer I cant really see this being more convenient than an array or other type of list. If you know of how to implement or access the stack pointer let me know.

  2. Madhu January 10, 2018 at 6:05 am - Reply

    Thank you for the great article but curious to know in which scenarios it will be useful when we are using PS.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: