Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Update current output line

is there a way to update the current line of the command output in powershell?

i know how to write to the same line:

Write-Host "hello " -NoNewLine

Write-Host "world!" -NoNewLine

but is there a way to complete replace the current line? something like:

Write-Host "hello "
Write-Host "world!" -ReplaceLine

thanks in advance!


SOLUTION:

Write-Host "hello " -NoNewLine
Write-Host "`rworld!"
like image 236
Dili Avatar asked Sep 06 '13 14:09

Dili


People also ask

How do I use AutoSize in PowerShell?

If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.

How do you go up a line in PowerShell?

To move to the beginning of a line, press Home . To move to the end of a line, press End .

What does :: mean in PowerShell?

From the about_Operators help topic: :: Static member operator Calls the static properties operator and methods of a .NET Framework class. To find the static properties and methods of an object, use the Static parameter of the Get-Member cmdlet. [


2 Answers

Use carriage return to return to the start of the line: `r

See this: http://ss64.com/ps/syntax-esc.html

And definition of carriage return: http://en.wikipedia.org/wiki/Carriage_return

If you want to clear a line, that and a load of spaces. Otherwise you can overwrite stuff.

I can't really write up an example here as it's SO's code character, but put it at the start of the "world" string and it should work.

Hope this helps!

like image 159
Mattsjo Avatar answered Oct 22 '22 13:10

Mattsjo


The solution is to use carriage return while writing to the host, as noted by others. Make sure to use the NoNewLine flag whith write-host or it will not work.

Carriage Return is acheived by using: `r

Here's a small example illustrating this answer:

$up = $true
$finish = $false
$timeout = 50
Write-Host "Updating" -NoNewline
do {
    for ($i=0;$i -le 100;$i++){
        if ($i -eq 100){$up = !$up}
        if ($up){
            $str = ""
            $x=99-$i
            for($z=0;$z -le $i;$z++){$str+="."}
            for($y=0;$y -le $x;$y++){$str+=" "}
            Write-host "`rUpdating$str" -NoNewline
            Start-Sleep -Milliseconds $timeout
        }else{
            $str = ""
            $x=99-$i
            for($y=0;$y -le $x;$y++){$str+="."}
            for($z=0;$z -le $i;$z++){$str+=" "}
            Write-host "`rUpdating$str" -NoNewline
            Start-Sleep -Milliseconds $timeout
        }
    }
    if ($timeout -le 0){$finish = $true}
    $timeout-=10
} until ($finish)
$str = ""
for ($i=0;$i -le 93;$i++){$str+=" "}
Write-Host "`rUpdate Complete!$str"
Read-Host "Press [ENTER] to close this Window"
like image 3
sh7411usa Avatar answered Oct 22 '22 13:10

sh7411usa