Today I want to quickly talk about the systems.collections.queue collection when working with powershell queues.

Why should we care about the system.collections.queue collection when working with powershell queues?

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 is just like a regular array, but is has some additional 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 when using the powershell queue (system.collections.queue):

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 can see all the 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  powershell queue collection.

Notice the order in which the strings are appearing.

Pretty straight forward I would say.

The DeQueue() method from powershell queue collection

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


In the example above,  “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.



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.


A note about using queues in a multithreaded environment

As pointed out by Johan Akerstrom, queues are very interesting when using runspaces, as it helps to keep track of what has to come next. with Queues in a multithreaded environment (which runspaces are) you need to first create the Queue and then get a Synchronized Thread Safe wrapped Queue by calling the Synchronized() static method on the System.Collections.Queue class.



Official MSDN Documentation –>  here
 Github issue explaining why $q | Get-Member doesn’t work –> here
That’s is all for today