Aug 19, 2017 01:39 PM
(This post was last modified: Aug 20, 2017 12:47 PM by Travelers.)
Been messing with this thing for about a day now. I'm by no means a coding expert so I'm pretty thrilled I was able to make my first port scanner. This thing only scans TCP ports, and you are able to define the ports/port range you want to scan.
I put plenty of comments in there, also. If you find any errors while running it, please let me know so I can correct them. Also feel free to tweak based on your needs or make your own improvements.
I put plenty of comments in there, also. If you find any errors while running it, please let me know so I can correct them. Also feel free to tweak based on your needs or make your own improvements.
Code
# PowerShell port scanner v 0.1.3
#
# added $scan.dispose()
#
# clear screen
cls
# change title window name
$host.ui.RawUI.WindowTitle = "Power Port Scanner"
# port range listed in an array
# define range of ports to scan
# using ".." creates a range to read from
$ports = 1..1024
# store the time in a variable
$dateTime = Get-Date
Write-Host "Welcome to the Power Port Scanner"
$remoteIP = Read-Host "What is the IP of the remote host to scan: "
Write-Host "Starting scan of $remoteIP at $dateTime"
# create Scanner Function
# use foreach loop to scan each port in $ports
Function Scanner {
foreach ($port in $ports)
{
try {
$scan = New-Object System.net.sockets.TcpClient
$scan.Connect($remoteIP, $port);
Write-Host "Port $Port is up"
}
# send all errors to oblivion
catch {
$_ > null
}
}
$scan.dispose()
}
Scanner
Write-Host "Scan completed at $dateTime"
# create an exit to the script
Write-Host "Press any key to continue ..."
# silently exit running script
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

![[Image: pia_banner_3.png]](https://www.privateinternetaccess.com/affiliates/banners/pia_banner_3.png)
