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

C
Replies
0
Views
99
Carlo Goretti
C
C
Replies
0
Views
140
Carlo Goretti
C
P
Replies
0
Views
105
proWare Solutions GmbH
P
C
Replies
0
Views
333
Carlo Goretti
C
Back
Top