Pages

Wednesday, August 12, 2020

ping multiple ip addresses from windows PowerShell script cmd

### save iplist in a txt file as "iplist.txt"

### save the script as "PingIP.ps1" ( powershell script )

### rightclick and choose "Run with PowerShell"


$names=Get-content "D:\tasks\Ping\iplist.txt"

foreach($name in $names){

if(Test-Connection -ComputerName $name -Count 2 -ErrorAction SilentlyContinue){

Write-Host "$name is up" -ForegroundColor Green

$output+="$name is up,"+"`n"

}

else{

Write-Host "$name is --------> down" -ForegroundColor Red

$output+="$name is --------> down,"+"`n"

}

}

$output | Out-File "D:\tasks\Ping\result.txt"

Start-Sleep -s 30



### iplist.txt ( without tab or space at end of line)

172.21.80.3

172.21.80.4

172.21.80.60

172.21.80.61

172.21.11.115

172.21.11.43

172.21.30.5

localhost



### result.txt ( looks like below )

172.21.80.3 is up,
172.21.80.4 is up,
172.21.80.60 is --------> down,
172.21.80.61 is up,
172.21.11.115 is up,
172.21.11.43 is up,
172.21.30.5 is up,
localhost is up,