Executing PowerShell command with parameters from C#

  • Thread starter Thread starter tim-othy555
  • Start date Start date
T

tim-othy555

Guest
Hello, I try to execute some PowerShell commands with parameters from c#.

This works fine:

using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("param($param1); Get-ChildItem $param1");
PowerShellInstance.AddParameter("param1", "C:\\");

Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

foreach (var item in PSOutput)
{
if(item != null)
{
Console.WriteLine(item.BaseObject.ToString());
}
}
}

But this does not work:


using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("param($param1); Get-ChildItem $param1");
PowerShellInstance.AddParameter("param1", "-Path C:\\");

...
}

Can someone help me to correct the last code. I would like to use the "-Path" parameter and later the "-Exclude" parameter like this in PowerShell:


Get-ChildItem -Path C: -Exclude *.exe

Continue reading...
 

Similar threads

Back
Top