Powershell – How to retrieve names of all private MSMQ queues – efficiently

commsmqnetpowershellwmi

How can I retrieve the names of all of the private MSMQ queues on the local machine, without using System.Messaging.MessageQueue.GetPrivateQueuesByMachine(".")? I'm using PowerShell so any solution using COM, WMI, or .NET is acceptable, although the latter is preferable.

Note that this StackOverflow question has a solution that returns all of the queue objects. I don't want the objects (it's too slow and a little flakey when there are lots of queues), I just want their names.

Best Answer

With Windows Server 2012 R2 you have the Message Queueing (MSMQ) Cmdlets installed, use the Cmdlet Get-MsmqQueue to get name and count.

Get-MsmqQueue | Select QueueName, MessageCount

Without Message Queueing (MSMQ) you can use Get-WmiObject

gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue | Select Name

Or over powershell remoting:

Invoke-Command -Session $sessions -ScriptBlock { Get-MsmqQueue | Select QueueName, MessageCount } 

or

$dataRaw = Invoke-Command -Session $sessions -ScriptBlock { gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue } 
$data | Sort-Object -Property PSComputerName,Name  | 
Format-Table  DisplayName, MessagesInQueue, PSComputerName