Overwriting Format-Table with PowerShell

I'm attempting to send the output of a while loop to a table that overwrites itself in the console upon each iteration of the loop.  Here is the script so far:

$array = @(
  'google.com'
  'yahoo.com'
  'reddit.com'
)

While ($true) {
  foreach ($Server in $Array) {
    $ping  = Test-Connection $server -Count 1
    if (!($ping)) {
      $status = 'Down'
    }
    else {
      $status = 'Up'
    }
    
    [array]$results += New-Object -TypeName PSObject -Property @{
      'Device Name' = $server
      'Status' = $status
      'IP Address'       = $ping.IPV4Address
      'Response Time' = $ping.ResponseTime
      }
    }
$results | Format-Table -AutoSize
}

Open in new window

The problem is the output isn't overwriting anything it is adding onto.  I realize this is because of the += used for the $results variable, I'm just not sure what to do about it.  Help?
LVL 17
Jason CrawfordTransport NinjaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael PfisterCommented:
Reset $results to $null at the start of the foreach loop:
While ($true) {
  $results = $null
  foreach ($Server in $Array) {
  

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael PfisterCommented:
And maybe a
Clear-Host
is required right before the $results | Format-Table -AutoSize

Clear-Host 
$results | Format-Table -AutoSize

Open in new window

Michael PfisterCommented:
Or, to reduce flicker, use setcursorposition

[console]::setcursorposition(0,0)

$array = @(
  'google.com'
  'yahoo.com'
  'reddit.com'
)
Clear-Host 
While ($true) {
  $results = $null
  foreach ($Server in $Array) {
    $ping  = Test-Connection $server -Count 1
    if (!($ping)) {
      $status = 'Down'
    }
    else {
      $status = 'Up'
    }
    
    [array]$results += New-Object -TypeName PSObject -Property @{
      'Device Name' = $server
      'Status' = $status
      'IP Address'       = $ping.IPV4Address
      'Response Time' = $ping.ResponseTime
      }
    }
[console]::setcursorposition(0,0) 
$results | Format-Table -AutoSize
}

Open in new window

Jason CrawfordTransport NinjaAuthor Commented:
Wow...I need sleep.  Thank you.
Michael PfisterCommented:
Start-Sleep $now for(at least 8h) ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.