Manipulate MSMQ with PowerShell

I´ve recently published an open source MSMQ component for the SQL Server Integration Services. While working on its implementation (and other, related things), I’ve created some PowerShell utilities for queue manipulation. Now I’m consolidating them and putting them here, too. I’ts just a single ps1 file.


A valid question. After all, there already are PS cmdlets that do the same thing, and more.

An advantage of this script is that while you have to install the MSMQ Cmdlets module, you only need to download the queue script and run it. (It doesn’t depend on those cmdlets, but uses the .NET System.Messaging.MessageQueue class internally.)

It’s also a lot simpler and isn’t meant as a replacement for those cmdlets – this is just a script that enables you to quickly do tasks you’re probably going to be doing a lot, while trying to get your data flowing.

What does it do?

You will always want to run it like this:

queue <command> <queue name>

These are the available commands:

  • exists – checks whether a queue exists.
  • create – creates a queue (if it doesn’t exist yet).
  • delete – deletes a queue.
  • purge – purges a queue (deleting all messages within).
  • spam – puts some string data of a random length in the message body, and a guid (as a string) in its label. Accepts an optional third parameter – the number of messages to generate. Default is 5.
  • count – outputs the number of messages currently in the queue.
  • dump – dumps the contents (label and body) of ALL the messages from a queue into output. (Note the messages aren’t removed from the queue.)

When the preconditions of an action aren’t strictly met (e.g. you’re trying to create a queue that exists, delete one that doesn’t, purging an empty queue, etc), no error will be thrown. If you want to see exactly what the script is doing, run it with the standard -verbose parameter.

There are usage examples right in the script – either read them there, or run help queue -examples.