Share via


PowerShell: Ping list of IP addresses

Introduction

This article will explain how to ping list of IP address in a single shot with PowerShell script. Note that we're using System.Net.NetworkInformation.Ping in these examples. However, you could also use the Test-Connection PowerShell cmdlet.

Applies To

The examples demonstrated below are tested with and apply to the following versions of SharePoint:

  • PowerShell

Copy all IP addresses in a text file

Copy all list of IP address in a text file line by line and save the text file with the name "IPAddresses.txt" in the script execution path.

PowerShell script to ping each IP address (with an external text file)

Open the power-shell command prompt and run below script by saving in .ps1 file.

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, ``"-"``, ([System.Net.NetworkInformation.Ping]::new().Send($_)).Status}

Stop-Transcript

PowerShell script to ping each IP address (with a collection variable)

Open the power-shell command prompt and run below script by saving in .ps1 file.

Start-Transcript -Path .\log.txt
@("1.1.1.1","2.2.2.2") | ForEach {Write-Host $_, "-", ([System.Net.NetworkInformation.Ping]::new().Send($_)).Status}
Stop-Transcript

The above script(in both scripts) generate the log file in the script execution path.

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, ``"-"``, ([System.Net.NetworkInformation.Ping]::``new``().Send($_)).Status}

Stop-Transcript

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, ``"-"``, ([System.Net.NetworkInformation.Ping]::``new``().Send($_)).Status}

Stop-Transcript

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, ``"-"``, ([System.Net.NetworkInformation.Ping]::``new``().Send($_)).Status}

Stop-Transcript

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, ``"-"``, ([System.Net.NetworkInformation.Ping]::``new``().Send($_)).Status}

Stop-Transcript