$Ports = "465" "587" "993" "995" $AllHosts = Get-Content C:\Users\MiAl\Documents\list_servers.txt ForEach($Hosts in $AllHosts) { $Hosts Foreach ($P in $Ports){ $check=Test-NetConnection $Hosts -Port $P -WarningAction SilentlyContinue If ($check.tcpTestSucceeded -eq $true) {Write-Host $Hosts.name $P -ForegroundColor Green -Separator " => "} else {Write-Host $Hosts.name $P -Separator " => " -ForegroundColor Red} } }