PowerShell : Create MSMQ

AT work I use MSMQ I fair bit, and I also use NServiceBus a fair bit of late, which thankfully takes care of creating all the queues needed. But, for those time when you really need to make a bunch of queues, it can be a time consuming exercise, so I decided to see if my new found PowerShell skills could automate this process (remember I am still learning, so this may not be the best/most current way, in fact  know for Windows 8.1/Windows Server there is a later more groovy API. But for now this is what I came up with







function printUsage() 
    Write-Host "Usage is CreateQueue.ps1 -queueName SomeQueuName 
		-isTransactional $true -isJournalEnabled $true 
		-userName mclocal\barbers -isAdminUser $true"

try {

    $fullQueueName = ".\private$\" + $queueName

    If ([System.Messaging.MessageQueue]::Exists($fullQueueName))
        Write-Host($fullQueueName + " queue already exists")
        $newQ = [System.Messaging.MessageQueue]::Create($fullQueueName, 
        if ($isJournalEnabled)
            $newQ.UseJournalQueue = $True

        if ($isAdminUser)
            Write-Host("NOT ADMIN")
catch [Exception] {
   Write-Host $_.Exception.ToString()


This will allow you to create a private queue of your choice of name, where you can also pick the following

  • Whether its a transactional queue
  • If journaling is enabled
  • If its an admin user queue

Hope it helps


One Comment

  1. Nice!

    Some minor things to make this more PowerShell idiomatic. Don’t use Write-Host so much. You’ll notice that pretty much none of the built-in cmdlets do. You could use Write-Verbose in most of the places you’re doing Write-Host. Make use of the pipeline. Put that $newQ object onto the pipeline for other commands to use. You could get really crazy by also providing default formatting for MessageQueue objects so that the output when typed on the command line would be nicer. That’s a pretty advanced thing to do, though and would require a separate blog post to explain. đŸ˜› Also, I’d suggest adding default values for many of those arguments and making them non-mandatory. About the only other suggestion I’d have would be to put this in a module so that you could load the assembly used here only when the module was imported rather than every time the cmdlet was run.

    Oh, I also just noticed your printUsage construct. That’s something you do typically in batch files and even in command line programs, but it’s not very PowerShell idiomatic. At the very least you could save yourself some code by simply leveraging Get-Help here rather than hard coding the printUsage function. You really won’t find any PowerShell cmdlets that do that, though. They just throw the exception (which goes to the error stream) and end. PowerShell users know to use Get-Help if they can’t understand why it failed from the exception message.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s