I'm just wondering if I can clear Out-Gridview on every loop like I can in the console:
while (1) { ps | select -first 5; sleep 1; clear-host }
Unfortunately this doesn't clear out-gridview every time:
& { while (1) { ps | select -first 5; sleep 1; clear-host } } | out-gridview
Clear-Hostclears the display of the host, which is the console window's content in a regular PowerShell console.By contrast,
Out-GridViewis a separate GUI window, over which PowerShell offers no programmatic display once it is being displayed.Notably, you can neither clear no refresh the window's content after it is displayed with the initial data.
The best approximation of this functionality is to close the old window and open a new one with the new data in every iteration - but note that this will be visually disruptive.
In the simplest case, move the
Out-GridViewinto the loop and call it with-Wait, which requires you to close it manually in every iteration, however:This answer shows how to implement an auto-closing
Out-GridViewwindow, but it is a nontrivial effort - and with a sleep period as short as1second it will be too visually disruptive.Ultimately, what you're looking for is a GUI version of the Unix
watchutility (or, more task-specifically, thetoputility).However, since you're not looking to interact with the
Out-GridViewwindow, there's little benefit to usingOut-GridViewin this case.Instead, you could just spawn a new console window that uses
Clear-Hostto display the output in the same screen position periodically:The following defines helper function
Watch-Outputto facilitate that:Note that this will still flicker every time the window content is refreshed. Avoiding that would require substantially more effort.
The
PowerShellCookbookmodule offers the sophisticatedWatch-Commandcmdlet, which not only avoids the flickering but also offers additional features.The big caveat is that - as of version
1.3.6- the module has several cmdlets that conflict with built-in ones (Format-Hex,Get-Clipboard,New-SelfSignedCertificate,Send-MailMessage,Set-Clipboard), and the only way to import the module is to allow the module's commands to override the built-in ones (Import-Module PowerShellCookbook -AllowClobber).