Deleting all NServiceBus MSMQ queues except error and audit

This post provides a PowerShell solution for removing MSMQ queues while preserving specific ones. I reference a prior tutorial on queue deletion and explain how to apply regex filtering to the process.

PowerShell Script

The core command leverages the System.Messaging namespace:

[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("LOCALHOST") | % {".\\" + $_.QueueName} | ? {$_ -notmatch "error|audit"} | % {[System.Messaging.MessageQueue]::Delete($_); }

Protected Queues

The script preserves queues matching these patterns:

  • audit
  • audit.log
  • error
  • error.log

This approach prevents disruption to ServiceControl's data import functionality by maintaining the error and audit queue infrastructure.