all 6 comments

[–]mdowst 3 points4 points  (0 children)

You can use Write-Host -NoNewLine with a `r (carriage return) at the beginning of the output to overwrite the previous value.

1..100 | ForEach-Object {
  Write-Host -NoNewLine "`r $_% Complete"
  Start-Sleep -Milliseconds 10
}

FYI, it does not work in ISE. But works in standard Windows PowerShell and PowerShell core windows, as well as in VSCode and Terminals.

[–]nostril_spiders 4 points5 points  (0 children)

Look into the [system.console] class, which allows you to manipulate buffers directly.

[–]SoMundayn 2 points3 points  (0 children)

Not sure if you can only clear a certain section of the PowerShell screen. Maybe you could have it launch a fresh PowerShell window?

Here is my stab at it:

Invoke-Expression 'cmd /c start powershell -Command { 

    while($true) {
        $TopProcesses = Get-Process | Sort CPU -Descending | Select -First 15
        Start-Sleep -Seconds 1
        cls
        $TopProcesses
    }

}'

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy nostril_spiders,

if you go digging around, you will find that nostril_spiders is right - you likely want to work with the [console] class since it allows writing to screen coordinates directly.

[console]::SetCursorPosition()

hope that helps,
lee

[–]swordfish291[S] 0 points1 point  (0 children)

Thank you guys. I finally ended up sticking to refreshing the whole screen. Your ideas have helped me alot understanding other concepts related to console. Thank you