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.
Format-Tableto which you implicitly pipe output does not know that you clear screen, and it just continue to print table, not printing new table.