Creating a Port scanner with Windows PowerShell

Spread the words

Create a simple port scanner using PowerShell.

Create a port scanner on a specific port on a specific network range.

$port = 80 (here I’m specifying the port to scan)

$network = “10.248.0” (here I’m specifying the network to scan)

$range = 1..254 (here I’m specifying the range to scan)

Foreach ($add in $range) { $ip = “{0}.{1}” –F $network,$add (here I’m calling the ip addresses one by one from the range)

If(Test-Connection –BufferSize 32 –Count 1 –quiet –ComputerName $ip) (here I’m pinging the ip using Test-Connection cmdlet)

 $socket = new-object System.Net.Sockets.TcpClient($ip, $port) (here I’m trying a connection on the port using System.Net.Sockets)

If($socket.Connected) { “$ip port open $port” $socket.Close()  (here is the outcome)

I also used the following parameters:

$ErrorActionPreference= ‘silentlycontinue‘ (silence any error during the scan)

Write-Progress (show the scanning progress)

Else (show a message if the port is not open)

Out-file (write the result to a file)

Now see the full script:

$port = (80)

$network = “10.248.0”

$range = 1..254

$ErrorActionPreference= ‘silentlycontinue’

$(Foreach ($add in $range)

{ $ip = “{0}.{1}” –F $network,$add

Write-Progress “Scanning Network” $ip -PercentComplete (($add/$range.Count)*100)

If(Test-Connection –BufferSize 32 –Count 1 –quiet –ComputerName $ip)

{ $socket = new-object System.Net.Sockets.TcpClient($ip, $port)

If($socket.Connected) { “$ip port $port open”

$socket.Close() }

else { “$ip port $port not open ” }

}

}) | Out-File D:\Tech\scan.csv

How it’s running:

scanport

And the result in a csv file:

csv

😉

Leave a comment

Your email address will not be published.


*