Capturing PowerShell output in C# after execution of each pipeline command Capturing PowerShell output in C# after execution of each pipeline command powershell powershell

Capturing PowerShell output in C# after execution of each pipeline command


I feel silly for answering my own question, but all I did was move the loop functionality from my C# code into my script and that worked. So now I pass all the keys and values at once as array parameters and only have the one command in the pipeline.

I would still be interested to know if it is possible to produce output after each command in the pipeline is executed, though.


I have a situation where I have an object that manages a PowerShell environment, and it takes a script or a module and cmdlet and executes it and then takes a new script or module and cmdlet or cmdlet from the original module and executes it. Each time something is executed, I need to return the results. I solved it by clearing the command queue after each execution:

powerShellInstance.Commands.Clear();

hope this helps.