Routing PowerShell Command Results to the Write-Output Cmdlet

I am working on a project where we host a PowerShell runspace in IIS and I needed to dump some output from a command using the Write-Output cmdlet. I futzed around for a while trying to execute a command in the middle of the Write-Output parameter something like what follows. This is my attempt to output the current working directory using a script block but it does not work because everything in quotes is taken as a string literal since the & character isn’t at the beginning of the line.

[sourcecode language=”powershell”]

Write-Output “Current directory is { & pwd }”

[/sourcecode]

I found a better way to do this by piping the output of the pwd command to the Write-Output cmdlet. This works well and gets me the output I need.

[sourcecode language=”powershell”]

pwd | Write-Output

[/sourcecode]

I may try to concatenate a string with the output so I can produce something like “Current directory is C:Program Files…” but this approach takes care of the most important need. I will update this post with any further details and feel free to comment if you have a different approach to this situation.

Comments are closed.

Sign in
classic
Forgot password?
×
Sign up

(*) Required fields

I agree with OptimaSales Terms & Privacy Policy

×