Read MSMQ from Powershell

<pre>
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")

cls

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

$msgs = $queue.GetAllMessages()
 
write-host "Number of messages=$($msgs.Length)" 

foreach ($msg in $msgs)
  {
      write-host $msg_.Id
      write-host $utf8.GetString($msg.BodyStream.ToArray())
  }

</pre>

Depending on how many times you wrote messages to the queue, the output will look something like this:

MSMQ_Read_Results

Uncategorized  

Leave a Reply