title: Windows Server 2012 - Access to registry denied when running powershell scripts from C#
body: <p>I'm building a C# application that has to run some powershell scripts.</p>
  
  <p>While the application works well on all my colleagues computers, I have issue having it to work properly on a Windows Server 2012 virtual machine.</p>
  
  <p>Whenever I run a script through the application I'm getting the following error message:</p>
  
  <blockquote>
    <p>Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".</p>
  </blockquote>
  
  <p>I precise that I went through a LOT of topics and tried many things that did not solve my problem
  I tried:</p>
  
  <p>set-executionpolicy unrestricted</p>
  
  <p>set-executionpolicy unrestricted -Scope -CurrentUser</p>
  
  <p>And of course I ran these commands on both x64 and x86 powershell instances (running the powershell application as administrator)</p>
  
  <p>Now the strange part is that I have no trouble running the script through the powershell window directly, the error only occurs when running the script through the C# application.</p>
  
  <p>Here's the code involved:</p>
  
  <pre><code>public static List&lt;string&gt; RunPowerShellScript(Sequence sq, Script script)
      {
          try
          {
              script.Results = new List&lt;Result&gt;();
  
              Runspace runspace = RunspaceFactory.CreateRunspace();
              runspace.Open();
              RunspaceInvoke runspaceInvoker = new RunspaceInvoke(runspace);
              //runspaceInvoker.Invoke("set-executionpolicy bypass");
              runspaceInvoker.Invoke("set-executionpolicy unrestricted");
  
              //Create a pipeline to send variables 
              Pipeline pipeline = runspace.CreatePipeline();
  
              //Create a command
              Command myCommand = new Command(Properties.Settings.Default.Scripts + "\\" + script.ScriptName + ".ps1", false);
              foreach (Parameter p in script.Parameters)
              {
                  CommandParameter param = new CommandParameter(p.ParameterName, sq.GetUpdatedParameter(p));
                  myCommand.Parameters.Add(param);
              }
  
              pipeline.Commands.Add(myCommand);
  
              //Return the output from the script
              Collection&lt;PSObject&gt; returnObjects = pipeline.Invoke();
              runspace.Close();
  
              List&lt;string&gt; output = new List&lt;string&gt;();
              foreach (PSObject po in returnObjects)
              {
                  if (po != null)
                      output.Add(po.ToString());
              }
  
              return output;
          }
          catch(Exception e)
          {
              return new List&lt;string&gt; { e.Message, "1" };
          }
      }
  </code></pre>