powershellnewlinewrite-host

How to preserve new lines from command output printed out using Write-Host in PowerShell?


I have recently (2-3 days ago) started to use Powershell. I have a C++ application that uses argparse to process some command line arguments. If something is not as expected, my code exits with the help string provided by -/--help flag of any application that uses this library.

I use (output from $PSVersionTable):

Name                           Value
----                           -----
PSVersion                      5.1.19041.2364
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.2364
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

I have a CMake configuration file

Write-Host "${APP_NAME} (${BUILD_TYPE})"

$model = ""
$data_in = ""

Write-Host "Checking input arguments"
if ($args.count -ne 5)
{
    Write-Host "Found {0} arguments but expecting 5" -f $args.count
    Write-Host $(./pytorch_load_model.exe --help) -foregroundcolor Cyan
    exit
}

for ( $i = 0; $i -lt $args.count; $i++ ) {
    if (($args[ $i ] -eq "-m") -or ($args[ $i ] -eq "--model")){ $model=$args[ $i+1 ]}
    if (($args[ $i ] -eq "-i") -or ($args[ $i ] -eq "--input")){ $data_in=$args[ $i+1 ]}
}

$fail = $false
if ($model -eq "")
{
    Write-Host "No path for model provided"
}
if ($data_in -eq "")
{
    Write-Host "No path for input provided"
}
if ($fail -eq $true)
{
    Write-Host (./model.exe --help)
    exit
}

# TODO Add check if $opencv and $libtorch valid dir paths
$opencv = "${OPENCV_BIN}"
$libtorch = "${LIBTORCH_BIN}"
#$model = "${PTH_MODEL_PATH}"
#$input = "${INPUT_IMG_PATH}"
Write-Host "Executing model"
$env:Path="$($env:Path);$($opencv);$($libtorch)"; ./pytorch_load_model.exe -m "$($model)" -i "$($data_in)"

which is processed by using configure_file() to output a script (*.ps1 file) that is added to the binary directory of my project (EXE, dlls and other runtime-relevant data). It sets up PATH so that the application runs as expected and also passes through the same command line argument as my application expects.

I have noticed that Write-Host "eats up" all of the new lines that come out as a multiline string from my application.

Instead of

PyTorch Load Model (release)
Checking input arguments
Usage: model [--help] [--version] [--model-path VAR] [--png-path VAR]

Optional arguments:
  -h, --help        shows help message and exits
  -v, --version     prints version information and exits
  -m, --model-path  Path to PTH TorchScript model
  -i, --png-path    Path to PNG image to be processed by the model

I receive

Usage: model [--help] [--version] [--model-path VAR] [--png-path VAR]  Optional arguments:   -h, --help        shows help message and exits    -v, --version     prints version information and exits    -m, --model-path  Path to PTH TorchScript model    -i, --png-path    Path to PNG image to be processed by the model

How do I preserve the new lines?


Solution

  • As @nimizen suggests, the best option seems to be piping: (./model.exe --help) | write-host

    As alternative workaround you can use ((./model.exe --help) -join "`r`n")