Avatar of Jason Crawford
Jason Crawford
Flag for United States of America

asked on 

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?
Powershell

Avatar of undefined
Last Comment
Michael Pfister

8/22/2022 - Mon