1 | initial version |
To execute PowerShell commands and scripts using an already existing PowerShell instance in C#, you can use the following steps:
using System.Management.Automation;
PowerShell ps = PowerShell.Create();
ps.AddScript("Get-Process");
var results = ps.Invoke();
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
ps.Dispose();
You can also use the PowerShell RunspacePool class to manage multiple PowerShell instances and configuration options.