title: Calling PowerShell cmdlet from C# throws CommandNotFoundException
body: <p>I'm trying to call the <code>Add-AppxPackage</code> cmdlet from C#.  I found the MSDN article on <a href="http://msdn.microsoft.com/en-us/library/dd182449%28v=vs.85%29.aspx" rel="nofollow noreferrer">running PowerShell from C# code</a>. I have referenced the System.Management.Automation assembly and have tried the following code snippets, all of which result in the same exception when trying to call powerShell.Invoke():</p>
  
  <blockquote>
    <p>System.Management.Automation.CommandNotFoundException was unhandled</p>
    
    <p>The term 'Add-AppxPackage' is not recognized as the name of a cmdlet,
    function, script file, or operable program. Check the spelling of the
    name, or if a path was included, verify that the path is correct and
    try again.</p>
  </blockquote>
  
  <p>Snippet 1:</p>
  
  <pre><code>var powerShell = PowerShell.Create();
  powerShell.AddCommand(string.Format("Add-AppxPackage '{0}'", appxFilePath));
  
  foreach (PSObject result in powerShell.Invoke())
  {
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>I understand why this doesn't work, since I shouldn't be providing parameters in the AddCommand() function.</p>
  
  <p>Snippet 2:</p>
  
  <pre><code>var powerShell = PowerShell.Create();
  powerShell.AddCommand("Add-AppxPackage");
  powerShell.AddParameter("Path", appxFilePath);
  
  foreach (PSObject result in powerShell.Invoke())
  {
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>Snippet 3:</p>
  
  <pre><code>var powerShell = PowerShell.Create();
  powerShell.AddCommand("Add-AppxPackage");
  powerShell.AddArgument(appxFilePath);
  
  foreach (PSObject result in powerShell.Invoke())
  {
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>My C# project targets .Net 4.5, and if I do powerShell.AddCommand("Get-Host") it works and the Version it returns back is 4.0.  Add-AppxPackage was added in v3.0 of PowerShell, so the command should definitely exist, and it works fine if I manually run this command from the Windows PowerShell command prompt.</p>
  
  <p>Any ideas what I am doing wrong here? Any suggestions are appreciated.</p>
  
  <p>-- Update --</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/17067971/invoking-powershell-cmdlets-from-c-sharp">this post</a> and <a href="https://stackoverflow.com/questions/13251076/calling-powershell-from-c-sharp">this one</a>, and realized there is a AddScript() function, so I tried this:</p>
  
  <p>Snippet 4:</p>
  
  <pre><code>var powerShell = PowerShell.Create();
  powerShell.AddScript(string.Format("Add-AppxPackage '{0}'", appxFilePath));
  
  var results = powerShell.Invoke();
  foreach (PSObject result in results)
  {
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>And it does not throw an exception, but it also doesn't install the metro app, and the "results" returned from powerShell.Invoke() are empty, so I'm still at a loss...</p>
  
  <p>-- Update 2 --</p>
  
  <p>So I decided that I would try just creating a new PowerShell process to run my command, so I tried this:</p>
  
  <pre><code>Process.Start(new ProcessStartInfo("PowerShell", string.Format("-Command Add-AppxPackage '{0}'; $key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyUp')", appxFilePath)));
  </code></pre>
  
  <p>but it still throws the same error that Add-AppxPackage is not a recognized cmdlet.</p>
  
  <h2>ANSWER</h2>
  
  <p>If you follow the long comment thread on robert.westerlund's answer, you will see that for some reason when running/launched from Visual Studio, PowerShell was not including all of the PSModulePaths that it does when running straight from a PowerShell command prompt, so many modules are not present. The solution was to find the absolute path of the module that I needed (the appx module in my case) using:</p>
  
  <pre><code>(Get-Module appx -ListAvailable).Path
  </code></pre>
  
  <p>And then import that module before trying to call one of its cmdlets. So this is the C# code that worked for me:</p>
  
  <pre><code>var powerShell = PowerShell.Create();
  powerShell.AddScript(string.Format(@"Import-Module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Appx\Appx.psd1'; Add-AppxPackage '{0}'", appxFilePath));
  var results = powerShell.Invoke();
  </code></pre>
  
  <h2>UPDATED ANSWER</h2>
  
  <p>You can see from <a href="https://stackoverflow.com/questions/22696155/powershell-does-not-load-all-psmodulepaths-when-running-from-visual-studio-cod">this other post I opened</a>, that the problem was with a bug in a Visual Studio extension (in my case StudioShell) causing not all of the PSModulePaths to be loaded. After uninstalling that extension all of the modules were loaded correctly and I no longer needed to manually import the module.</p>