title: Powershell delete MSMQ remotely
body: <p>I was wondering if it was possible to delete queues remotely via PowerShell? I have the following script:</p>
  
  <pre><code>cls
  
  [Reflection.Assembly]::LoadWithPartialName("System.Messaging")
  
  $computers = @("comp1","comp2","comp3");
  
  foreach($computer in $computers) {
  
      $messageQueues = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine($computer);
  
      foreach ($queue in $messageQueues) {
          $endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);
  
          Write-Host $endpoint
          [System.Messaging.MessageQueue]::Delete($endpoint);        
      }
  }
  </code></pre>
  
  <p>This works fine, if I was running it on the machine whose queues I want to delete however when I run this remotely I get the error:</p>
  
  <pre><code>The specified format name does not support the requested operation. For example, a direct queue format name cannot be deleted.
  </code></pre>
  
  <p>Any ideas if this can be done?</p>
  
  <p><em>EDIT</em></p>
  
  <p>Oddly, I have figured I can remote onto the machine via PowerShell and execute a script block. However, I don't understand the difference between doing:</p>
  
  <p>THIS:</p>
  
  <pre><code>$endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);
  Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete($endpoint) };
  </code></pre>
  
  <p>AND THIS:</p>
  
  <pre><code>Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete("FormatName:DIRECT=OS:MY_SERVER\some.endpoint") };
  </code></pre>
  
  <p>The value of <code>$endpoint</code> is the same however, for some odd reason it doesn't like the variable approach though both values are identical. I tested this by setting <code>$endpoint</code> then calling delete. I get the error:</p>
  
  <pre><code>Exception calling "Delete" with "1" argument(s): "Invalid value  for parameter path."
  </code></pre>
  
  <p>What I'm trying to say is if I hard code the value as part of the argument it works but assign it to a variable then invoke the method I get an error </p>