1

If I run ng build -environment=localprod --base-href /Angular5Starter/ in command prompt I receive the following output:

Command prompt output

However, if I run the same command from Powershell:

$ngBuildCommand = "ng build -environment=$($environment) --base-href /$($webSiteName)/"
Write-Host "Ng build command: ", $ngBuildCommand
cmd /c $ngBuildCommand

I receive a quite messed up output:

Powershell output


I have followed the suggestions from the comments and ran the command properly:

$ngExe = "C:\Program Files\nodejs\ng.cmd"
& $ngExe build -environment="$environment" --base-href /$webSiteName/

However, the output text format is the same.

Question: Is it possible to catch cmd.exe output format (and maybe colors) in Powershell?

9
  • Is there a reason you are using powershell to run cmd.exe? Just run the command directly. One way & $ngBuildCommand Commented Apr 19, 2018 at 12:55
  • @Appleoddity - This is part of a larger script with deals with starting and stopping IIS application pools and Powershell is helping a lot in this area.
    – Alexei
    Commented Apr 19, 2018 at 13:00
  • 1
    Again, the purpose of powershell is to completely replace cmd.exe. There is nothing in cmd.exe you can’t do in powershell. You shouldn’t be running cmd.exe to simply execute an .exe file which is what this line of code does. This is not good scripting. Commented Apr 19, 2018 at 13:03
  • 1
    Give the path to the executable and include the .exe on the end.
    – EBGreen
    Commented Apr 19, 2018 at 13:06
  • 1
    See this: stackoverflow.com/a/30178582/8928481
    – Biswapriyo
    Commented Apr 20, 2018 at 12:24

1 Answer 1

1

I have realized that my post is an XY problem. What I really want is readable output, not colored one.

I have tried Biswapriyo's solution and it works well except for two things:

  • colors are changed (not a big deal)
  • some strings (near the { }) are missing

What seems to solve the problem is to actually strip the colors from angular-cli's output, as indicated here:

$ngExe = "C:\Program Files\nodejs\ng.cmd" 
$executionRes = & $ngExe build -environment="$environment" --base-href /$webSiteName/ | %{$_ -replace "\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]"," "} | %{$_ -replace "     ", "`n"}
Write-Host $executionRes

This will strip all colors and keep the original text intact, except from some extra whitespace here and there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .