ps1 cannot be loaded because running scripts is disabled on this system : <p>I try to run <code>powershell</code> script from c#.</p>

<p>First i set the <code>ExecutionPolicy</code> to <code>Unrestricted</code> and the script is running now from <code>PowerShell ISE</code>.</p>

<p>Now this is c# my code:</p>

<pre><code>class Program
{
    private static PowerShell ps;
    static void Main(string[] args)
    {
        ps = PowerShell.Create();
        string ps1File = Path.Combine(Environment.CurrentDirectory, "script.ps1");
        ExecuteScript(ps1File);
        Console.ReadLine();
    }

    static void ExecuteScript(string script)
    {
        try
        {
            ps.AddScript(script);
            Collection&lt;PSObject&gt; results = ps.Invoke();
            Console.WriteLine("Output:");
            foreach (var psObject in results)
            {
                Console.WriteLine(psObject);
            }
            Console.WriteLine("Non-terminating errors:");
            foreach (ErrorRecord err in ps.Streams.Error)
            {
                Console.WriteLine(err.ToString());
            }
        }
        catch (RuntimeException ex)
        {
            Console.WriteLine("Terminating error:");
            Console.WriteLine(ex.Message);
        }
    }
}
</code></pre>

<p>And the output is:</p>

<blockquote>
  <p>ps1 cannot be loaded because running scripts is disabled on this
  system. For more informationm see about_Execution_Policies at
  <a href="http://go.microsoft.com/fwlink/?LinkID=135170" rel="noreferrer">http://go.microsoft.com/fwlink/?LinkID=135170</a>.</p>
</blockquote>
