The Linux watch
command allows one to run the same command repeatedly over a specified interval and see the output on the screen. Additional switches allow highlighting differences as the output changes. This capability would be useful to have a Watch-Command
. watch
works against text, so one design consideration is would -diff
only apply to the text output after it passes through formatting or the entire set of objects (which would be more resource intensive if there are nested objects and difficult to display).
watch
doesn't exist on Windows, so this would enable watch
to work on Windows while Linux users can use Watch-Command
against cmdlets (well, whole scriptblocks).
Could this work, by default against text, but optionally against the entire set of objects. By default take the light-weight approach, but let me specify a more resource intensive solution available I needed it?
First of all, would like to say I like the idea, I personally find myself creating "oneliners" like while($true) { stuff; start-sleep 5 }
a lot, so I think it would be great to have a parameter like that for when I monitor a condition in real time.
Other than that I think if at all possible the parameter should be kept the same for text and objects, maybe limiting how much it traverses an object as a "solution" to limit performance/display issues. Personally speaking, I usually don't dig too deep in the objects, and since I control the scriptblock, spending a little extra time to simplify the return object, doesn't seem too big a deal in my opinion.
@thlac - yes, exactly, This was the exact thing that we were discussing on an email thread inside of MS that initiated this issue. While a -watch parameter could be interesting as mentioned above it may not be applicable in all cases. Personally, I think this is a big gap in the terminal experience of Windows and would love to see it added as a cmd in Windows out of the box because (a) it is useful beyond the scope of just PowerShell - e.g. watch dir and (b) it would smooth out the surface area between Windows and Linux/macOS for people that are using both or are migrating.
This capability would be useful to add to Invoke-Command
The cmdlet is very complex - huge parameters and parameter sets.
I think more simple and more discoverable to create a new cmdlet like:
Invoke-Watch [-ScriptBlock] <scriptblock> [[-Interval] <int>] [-Difference]
watch works against text, so one design consideration is would -diff only apply to the text output after it passes through formatting or the entire set of objects (which would be more resource intensive if there are nested objects and difficult to display).
I agree that this could be in Formatting System. There might be interesting scenarios.
Currently Format-Table does already specific thing - delay and collect object before starting output.
So Formatting System would keep a state. It says that we could add -Diff
to Formatting System.
As result not only Invoke-Watch
could benefit from this.
watch
by definition does "runs command repeatedly, displaying its output". It is a cycle. We haven't such cmdlet but we could.
Our Test-Connection cmdlet can do a cycle with -Repeat
switch (and ). We could generalize this like:
Invoke-Repeat [-ScriptBlock] <scriptblock> [[-Delay] <int>] [[-Count] <int>]
In the case Invoke-Watch could be implemented as
Invoke-Repeat $sb | Format-* -Difference
What about Watch-Command
? It's not a super frequently used verb, but it is on the list.
@SeeminglyScience great suggestion, updated title and description rather than adding more parametersets to cmdlets :)
We need more Watch-*
cmdlets. That's much nicer.
(Anyone down for Watch-Item
to wrap things like filesystemwatcher and make them a bit easier to handle?)
@vexx32 open a new issue to discuss that :)
I'd love to have this! I actually have this hacked together in a function in my profile using Start-Sleep
and Clear-Host
. The problem with that though is that there is a flicker because it first clears the screen, then draws the new content. It would be much nicer to have an optimized cmdlet that just overrides the content in place without flicker.
I also wonder if there could be a way for _custom_ commands to return a "stream of updates". There are many non-generic commands that do this, like kubectl watch
or kubectl rollout status
. PowerShell equivalents currently have no good way to express this. Maybe a switch could be added to Out-Host
and Format.ps1xml files to enable having every new stream item override the previous output? HighlightDifferences
would then also be a switch that could be used together with it. Maybe those flags should also be present on CmdletBinding()
to allow setting it without having to modify Formatting, for the use case of Watch-Command
. I think this kind of system would be useful for other features too, like always making certain commands page (like Get-Help
, or a PowerShell git log
equivalent).
Most helpful comment
What about
Watch-Command
? It's not a super frequently used verb, but it is on the list.