Quantcast
Channel: Active questions tagged powershell-3.0 - Stack Overflow
Viewing all articles
Browse latest Browse all 123

Remove last already printed to console line in Powershell

$
0
0

I am calling a command like this:

Write-Host 'Calling myScript... ' -NoNewlineInvoke-Expression '& .\myScript.bat arg1 arg2 arg3'If ($LASTEXITCODE -eq 0) { Write-Host 'done' }Else                     { Write-Host '`nERROR' }

I need to be able to both give command line arguments and to have access to $LASTEXISTCODE

.\myScript.bat can either produce an error output (which I want to see if it occurs) or, if all is ok, it seems to only produce a single newline as output. (unfortunately I cannot look deeper into myScript.bat because it calls a large project developed by another team, so I have to work around these empirical observations)

When no error occurs I do not want to remain with that newline in the middle of my output, I want the 'done' to be printed on the same line as the 1st Write-Host (and only the ERROR to be printed on a new line if it happens)

So essentially I need to "go back" to the previous line, "delete" the line that was created by the newline printed when myScript.bat executed with no error.

My goal is to maybe get something like this:

Write-Host 'Calling myScript... ' -NoNewlineInvoke-Expression '& .\myScript.bat arg1 arg2 arg3'If ($LASTEXITCODE -eq 0) { SetCursorToPreviousLine ; Write-Host 'done' }Else                     { Write-Host '`nERROR' }

Viewing all articles
Browse latest Browse all 123

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>