title: Running .ps1 Powershell from C# - Not Working
body: <p>This is not new for most here. I went through almost all posts from here and blogs. It doesn't error out.</p>
  <p><em><strong>I am not getting any errors. If  I run PowerShell locally it works [expected to take screen shot], when running from C# it is not taking Screenshots.</strong></em></p>
  <p>Installed all the required NuGet packages.
  <code>using System.Management.Automation;</code></p>
  <p>I am trying to run .ps1 from c# but not working. Powershell takes a screenshot and stores it in a provided path.</p>
  <p><strong>Locally I am running .ps1 as below and it takes a screenshot. But from c# it doesn't.</strong></p>
  <pre><code>C:\source\Hooks\TestScreenCapture.ps1 -ScreenShotPath C:\source\Hooks\testy.png
  </code></pre>
  <p>This function takes a screenshot when runs from selenium.</p>
  <p>I have written a c# code to run it.</p>
  <p><strong>Script located at:</strong></p>
  <pre><code>var psScript = Path.Combine(AppDomain.CurrentDomain.BaseDirectory.Replace(&quot;bin\\Debug\\netcoreapp3.1&quot;, &quot;Hooks&quot;), &quot;TestScreenCapture.ps1&quot;);
  </code></pre>
  <p><strong>1st Approach:</strong></p>
  <pre><code>var startInfo = new ProcessStartInfo()
              {
                  FileName = &quot;powershell.exe&quot;,
                  Arguments = $&quot;-NoProfile -ExecutionPolicy unrestricted -file \&quot;{psScript}\&quot; -ScreenShotPath \&quot;{screenShotFullPath}\&quot;&quot;,
                  UseShellExecute = false
              };
              Process.Start(startInfo);
  </code></pre>
  <p><strong>2nd Approach</strong></p>
  <pre><code>var powerShell = PowerShell.Create();
  powerShell
            .AddScript($&quot;\&quot;{psScript}\&quot;&quot;, false)
            .AddParameter(&quot;ScreenShotFullPath&quot;, screenShotFullPath)
            .Invoke();
  </code></pre>
  <p><strong>3rd Approach</strong></p>
  <pre><code>var powerShell = PowerShell.Create();
  powerShell
            .AddScript(@&quot;&amp; {psScript} -ScreenShotFullPath {screenShotFullPath}&quot;)
            .Invoke();
  </code></pre>
  <p><em>I tried running PowerShell code from my C# just to check if c# is working or not and it works.</em></p>
  <pre><code>string criteria = &quot;system*&quot;;
  PowerShellInst.AddScript(&quot;Get-Service -DisplayName &quot; + criteria);
  Collection&lt;PSObject&gt; PSOutput = PowerShellInst.Invoke();
  foreach (PSObject obj in PSOutput)
  {
      if (obj != null)
      {
          Console.Write(obj.Properties[&quot;Status&quot;].Value.ToString() + &quot; - &quot;);                
          Console.WriteLine(obj.Properties[&quot;DisplayName&quot;].Value.ToString());
      }
  }
  Console.WriteLine(&quot;Done&quot;);
  </code></pre>
  <p><strong>Issues seem PowerShell to take a screenshot from c# is not working.</strong></p>