title: How to Pass PSCredential object from C# code to Powershell Function
body: <p>I have a PSCredential Object in C# and want to pass it as parameter for this PowerShell Script</p>
  
  <p>This is the PSCredential Object</p>
  
  <pre><code>    PSCredential Credential = new PSCredential ( "bla" , blasecurestring)
  </code></pre>
  
  <p>This is the Script I want to run in C#</p>
  
  <pre><code>powershell.AddScript("$s = New-PSSession -ComputerName '" + serverName + "' -Credential " + Credential);
  </code></pre>
  
  <p>I couldn't understand the solution which is offered here <a href="https://stackoverflow.com/questions/3961192/pass-a-parameter-object-pscredential-inside-a-scriptblock-programmatically-in">Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#</a></p>
  
  <p>EDIT:
  This thing is working</p>
  
  <pre><code>powershell.AddCommand("New-PSSession").AddParameter("ComputerName", serverName).AddParameter("Credential", Credential);
  </code></pre>
  
  <p>But how can I save the Session Info in a variable? I need them for the following commands:</p>
  
  <pre><code>powershell.AddScript(@"Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");
  </code></pre>