You have a syntax error in the commands that you are sending to PowerShell.
Change your code a bit so you can see the error message from PowerShell.
In Visual Studio Debug mode, capture the value of "c" just before you assign it to startinfo.arguments and use it in an interactive PowerShell window to determine the exact cause of your syntax error.
Here is how I revised your code to see the PowerShell error:
...
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
StreamReader sr = process.StandardOutput;
while (! sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
};
Here is the PowerShell error:
+ ... \\10.0.4.41\B$B" -Recurse -Force
+ ~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression
Added to solution
I think the problem may be related to the fact that C# uses \ character as an escape character so you end up putting in all these extra \ characters. When you copy the "c" variable to the ClipBoard and try to use that in PowerShell, PowerShell does not use \ as an escape character.
Therefore, I wonder, for debugging, if it would be a good idea to do the following:
1. In PowerShell develop a script that works 100% of the time.
2. Use that script as the basis to develop C# code to generate the same script putting in the extra \ characters where needed.
---- The following is for developing and debugging - It will not be part of your final program --
3. Have your C# program write the PowerShell commands to a text file (.txt)
4. Open the .txt file in NotePad (interactively) and copy the PowerShell commands to the ClipBoard
5. Open PowerShell (interactively) and paste the commands into PowerShell to test them.
6. Repeat steps 3-5 until you have C# code that generates a set of PowerShell commands that work correctly.
---- When the C# generated commands work correctly ----
7. Remove the creation of the .txt file from your C# program.