powershell system.collections.queue
Today I want to quickly talk about the systems.collections.Queue collection.

why should we care about the system.collections.Queue collection

Working with powershell, generally a simple array declared and used like this will generally be enough.

We then need to access our array via the index

Although this is trivial, and very often enough for our daily business (or in other words, we will always find a way to make it work). there are sometime some other (better?) ways to do things.
The System.Collection.Queue ist like a regular array, but is has some convenient methods that allow us to always return the first element added to our queue.
The System.Collection.queue follows the principle of first in, first out.

Methods and properties you don’t want to miss:

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

Pretty surprisingly, when you try to look at the members that are available you will find your self in front of the following error:

I’ll explain a bit later what really happened here, but for now, know that although we have had this error message, and when we look into our $q variable it seems empty, the instance has been created.  We will can see it members if we use the -inputObject parameter, as in the example below.

 

We will focus on the three most interesting ones of the Queue collection:
  • Enqueue
  • Dequeue
  • peek

The .Enqueue() method of the system.collections.Queue

 

The enqueue method is the method that will allow us to add new information to our queue.

Notice the order in which the strings are appearing.

Pretty straight forward I would say.

The DeQueue() method

The dequeue method is the method that will return the next element of our queue.

Remember, the systems.collections.Queue is a type of collection that respects the first in, First out principle

 

As you can see, “hi” is the first element to be returned when we called the DeQueue() method.

When we look again in our variable, we can see that the value hi is not more present anymore, but “how are” and “you” are still there.

 

 

The Peek() method:

The peek method will work exactly as the Dequeue() method, except,  that the item that was returned will not be removed from the Queue. As it’s name suggest, it allows you to peek into the queue, and to see what would  be the next returned element if you would call the DeQueue() method.

 

 

As you can see in the example above, the peek method (in red) returns the item, but doesn’t removes it from the 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.

Links

Official MSDN Documentation –>  here
 Github issue explaining why $q | Get-Member doesn’t work –> here
That’s is all for today
By | 2018-01-03T15:00:30+00:00 January 3rd, 2018|PowerShell|1 Comment

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.

One Comment

  1. crshnbrn66 January 3, 2018 at 3:09 pm - Reply

    Nice article I’ll for sure book mark this for later use.

Leave a Reply

%d bloggers like this: