0

I wrote the following function as a rudimentary way of implementing basic functionality of the *nix watch command within Powershell:

function watch {
    Param(
        [Parameter(Mandatory=$true)][string]
        $command,
        [Parameter(Mandatory=$false)][int]
        $n = 2
    )

    while($true) {
        clear
        Write-Output (iex $command)
        sleep $n
    }
}

When working with cmdlets that return Powershell objects, I get strange behavior. For example if I run `watch 'get-command ls', on the first iteration I get the following formatted output of the object:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem

But on the second and subsequent iterations, it truncates the object property headers (and any description above it in certain other commands):

Alias           ls -> Get-ChildItem

I'm curious as to why this behavior happens, and how I can make the output identical to the first iteration for all subsequent iterations. I am running this in Powershell 5.1 on Windows 10.

1
  • Because Format-Table to which you implicitly pipe output does not know that you clear screen, and it just continue to print table, not printing new table. Commented Oct 21, 2016 at 5:06

1 Answer 1

1

I think it's because you're mashing up Write-Output which writes to the pipeline, and clear which is properly named Clear-Host and clears the local terminal, and knows nothing of the pipeline. It goes better with Write-Host.

Since your function loops eternally, the pipeline output never finishes, so what you would get get is this endless list with one set of headings:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           ls -> Get-ChildItem
Alias           ls -> Get-ChildItem
Alias           ls -> Get-ChildItem
Alias           ls -> Get-ChildItem
Alias           ls -> Get-ChildItem
Alias           ls -> Get-ChildItem

but you clear the /display/ in the middle of the list, so it carries on printing the subsequent items without the headings.

If you explicitly write to Format-Table in your command you can get the headings repeated:

watch { Get-Alias ls | Format-Table }
Sign up to request clarification or add additional context in comments.

2 Comments

I actually got the same behavior without combining write-output and iex, I combined write-output thinking it might fix the issue, but still got the same behavior. I'll give adding format-table a shot
Good news, Format-Table was exactly what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.