title: Powershell, Service Bus For Windows Server Programmatically: Command found, module could not be loaded
body: <p>From C# code I'm trying to retrieve all the namespaces from powershell... (Later more complex things, like creating namespaces)</p>
  
  <pre><code>PowerShell ps = PowerShell.Create();
  ps.AddCommand("Import-Module").AddArgument("ServiceBus").Invoke();
  var result = ps.AddCommand("Get-SBNamespace").Invoke();
  </code></pre>
  
  <p>Above code gives the following exception: </p>
  
  <blockquote>
    <p>The 'Get-SBNamespace' command was found in the module 'ServiceBus',
    but the module could not be loaded. For more information, run
    'Import-Module ServiceBus'.</p>
  </blockquote>
  
  <p>Does anyone know how to solve this error?</p>
  
  <p><strong>CURRENT STATUS:</strong> after some debugging I've found that no modules are loaded by default in the PowerShell object. Using the code:</p>
  
  <pre><code>InitialSessionState iss = InitialSessionState.CreateDefault();
  iss.ImportPSModule(new string[]{@"serviceBus"});
  PowerShell ps = PowerShell.Create(iss);
  </code></pre>
  
  <p>doesn't work to load the service bus module. Also the code:</p>
  
  <pre><code>ps.AddCommand("Import-Module").AddParameter("-Name", "serviceBus").Invoke();
  </code></pre>
  
  <p>doesn't work to import the service bus module. Running Visual Studio in administrator mode also doesn't make a difference</p>
  
  <p>Thanks in advance</p>