Capturing Powershell output in C# after Pipeline.Invoke throws

Not sure if this is helpful. I am guessing you are running V1. This V2 approach doesn’t throw and prints the result: Hello World 67 errors string script = @” ‘Hello World’ ps | % { $_.name | out-string1 } “; PowerShell powerShell = PowerShell.Create(); powerShell.AddScript(script); var results = powerShell.Invoke(); foreach (var item in results) … Read more

How can I find the source path of an executing script? [duplicate]

The ubiquitous script originally posted by Jeffrey Snover of the PowerShell team (given in Skyler’s answer) and the variations posted by Keith Cedirc, and EBGreen, all suffer from a serious drawback–whether the code reports what you expect depends on where you call it! My code below overcomes this problem by simply referencing script scope instead … Read more

How to perform keystroke inside powershell?

If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application? $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate(‘title of the application window’) Sleep 1 $wshell.SendKeys(‘~’) If that interactive application is a PowerShell script, just use whatever is in the title bar of the PowerShell window as the argument to AppActivate (by … Read more