Write MSMQ Messages from PowerShell

This PowerShell script sends a message to an MSMQ. I did it this particular way because NServiceBus can consume messages sent like this.

[Reflection.Assembly]::LoadWithPartialName("System.Messaging");

$queueName = '.\Private$\your.queue.name';
$queue = new-object System.Messaging.MessageQueue $queueName;
$utf8 = new-object System.Text.UTF8Encoding;

$tran = new-object System.Messaging.MessageQueueTransaction;
$tran.Begin();

$msgContent = '<?xml version="1.0" ?>
   <Messages xmlns="your-xml-namespace">
        <IWebApplicationStartupCompleting>
        </IWebApplicationStartupCompleting>
    </Messages>';
$msgBytes = $utf8.GetBytes($msgContent);

$msgStream = new-object System.IO.MemoryStream;
$msgStream.Write($msgBytes, 0, $msgBytes.Length);

$msg = new-object System.Messaging.Message;
$msg.BodyStream = $msgStream;
$queue.Send($msg, $tran);

$tran.Commit();
This entry was posted in CodeMinder, MSMQ, PowerShell, Programming. Bookmark the permalink.
  • I also like to set the message label, so that anyone who looks in a message queue and sees the message will know what it is without having to open it. Simply add .Label = ‘your label’; after the .BodyStream line.

  • Excellent post, helped me avoid writing a console app which just dumped a simple message on the bus. Thanks.

    • Jason

      Glad to hear that, Antony – thanks for dropping me a note!

  • AaronD12

    Here it is cleaned up:

    [Reflection.Assembly]::LoadWithPartialName(“System.Messaging”);

    $queueName = ‘.Private$your.queue.name’;
    $queue = new-object System.Messaging.MessageQueue $queueName;
    $utf8 = new-object System.Text.UTF8Encoding;

    $tran = new-object System.Messaging.MessageQueueTransaction;
    $tran.Begin();

    $msgContent = ”;
    $msgBytes = $utf8.GetBytes($msgContent);

    $msgStream = new-object System.IO.MemoryStream;
    $msgStream.Write($msgBytes, 0, $msgBytes.Length);

    $msg = new-object System.Messaging.Message;
    $msg.BodyStream = $msgStream;
    $queue.Send($msg, $tran);

    $tran.Commit();

    • Thanks, AaronD12 – not sure when that got ugly. I cleaned it up in the original.

  • ???

    Any idea why this code might fail to do anything at all?

    I’m on an old WS2K8R2 system, and have to administrate MSMQ with PowerShell scripts. I’m doing various other things related to reading, receiving, deleting and analysing messages in a queue with PowerShell scripts using System.Messaging. However, this code to send a message (and various other examples I’ve found around the web) don’t have any effect. I get no error message or anything, the queue simply stays empty. There is no one consuming messages from the queue either (I used journalling to make sure).

    • Sorry, I have no idea why one would get that behavior — no error on either the .send() or the .commit(), and no result either, seems very strange! Good luck figuring it out, and if you learn something that could improve this code, please let me know.

      • ???

        Hi Jason, thanks for the reply! As it turns out, the problem was the queue itself. It wasn’t set up as a transactional queue, which I wasn’t aware of. Unfortunately, there doesn’t seem to be a way to transform an existing non-transactional queue into a transactional one, so I had to create a new queue and migrate the system over. Now, code such as the example in your post works fine! I still find it a bit irritating, though, that for a non-transactional queue, there is no feedback at all and messages are just silently dropped…

        • Ah, thanks for letting me know! Silent failures are the worst. I’ll stick that piece of knowledge in my back pocket.