title: Test-connection to network printers
body: <p>I'm trying to get the online or offline status of all network printers using the powershell. I have two simple codes: </p>
  
  <pre><code>$servers = "myserver1", "myserver2"
  gwmi win32_printer -ComputerName $servers | Where-Object {$_.shared} | %{ $prname = $_.Name; $prtname = $_.portname; $srv = $_.systemname; $status = $_.detectederrorstate; $location = $_.location; gwmi win32_tcpipprinterport -computername $servers | where { $_.Name -eq $prtname } | select @{name="Name";expression={$prname}}, @{name="Server";expression={$srv}}, @{name="Status";expression={$status}}, @{name="Location";expression={$location}}, hostaddress }
  </code></pre>
  
  <p>Which makes output like:</p>
  
  <pre><code>Name        : printer_name
  Server      : printer_server
  Status      : detectederrortate
  Location    : room_number
  hostaddress : printer_fqdn
  </code></pre>
  
  <p>And </p>
  
  <pre><code>$pp= gwmi win32_tcpipprinterport | select hostaddress 
  foreach ($p in $pp) {
  $pr = $p.hostaddress
  if (Test-Connection -ComputerName $pr -BufferSize 16 -Count 1 -Quiet) {Write-Host "$pr is online"}
  
  else
  {write-host "$pr is offline"}
  }
  </code></pre>
  
  <p>With output</p>
  
  <pre><code>printer_fqdn is offline
  printer_fqdn is online
  </code></pre>
  
  <p>How can I get output like this</p>
  
  <pre><code>Name        : printer_name
  Server      : printer_server
  Status      : 0
  Location    : room_number
  hostaddress : printer_fqdn
  Ping        : Online\Offline
  </code></pre>
  
  <p>using test-connection cmdlet and paste the result of 2nd script into the new string with ping results?</p>