-2

Wondering the way to pass a console application output from cmd.exe to Powershell for further manipulation without a temp file.

Example:

cmd /c SET | powershell -command Write-Host -f Green $_

Expectation: the green-colored SET's output.

etix
  • 187
  • 1
  • 5
  • Wait, are you running this in PowerShell or cmd? – Abraham Zinala Oct 07 '21 at 17:18
  • 3
    `-Command "$input | Write-Host -f Green"`. Do note that this particular example is contrived, of course, since PowerShell can simply read the environment itself and format it the way `SET` does, if you like (`dir env:\ |% { $_.name + "=" + $_.value }`). – Jeroen Mostert Oct 07 '21 at 17:23
  • There is not the question about the coloring itself or about the env variables. This is about the approach to pass data to powershell for further processing. In any case, the question is answered. Thank you all. – etix Oct 07 '21 at 17:39

1 Answers1

3

If you want to pipe the output from an executable from within powershell, simply pipe directly to a function:

cmd /c SET |Write-Host -ForegroundColor Green

If you want to pipe output to powershell.exe from cmd.exe:

SET |powershell -Command "$input |Write-Host -ForegroundColor Green"

Alternatively, grab the environment variables directly from PowerShell's env: drive, no need to invoke cmd:

Get-ChildItem env: |Out-String |Write-Host -ForegroundColor Green
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206