Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Test Port

Posted on January 9, 2017 By David Kittell

Similar to CMD Telnet this PowerShell will test a port for you based on certain parameters.

function Test-Port
{
    Param(
        [parameter(ParameterSetName='ComputerName', Position=0)]
        [string]
        $ComputerName,

        [parameter(ParameterSetName='IP', Position=0)]
        [System.Net.IPAddress]
        $IPAddress,

        [parameter(Mandatory=$true , Position=1)]
        [int]
        $Port,

        [parameter(Mandatory=$true, Position=2)]
        [ValidateSet("TCP", "UDP")]
        [string]
        $Protocol
        )

    $RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};

    If ($Protocol -eq 'TCP')
    {
        $test = New-Object System.Net.Sockets.TcpClient;
        Try
        {
            Write-Host "Connecting to "$RemoteServer":"$Port" (TCP)..";
            $test.Connect($RemoteServer, $Port);
            Write-Host "Connection successful";
        }
        Catch
        {
            Write-Host "Connection failed";
        }
        Finally
        {
            $test.Dispose();
        }
    }

    If ($Protocol -eq 'UDP')
    {
        Write-Host "UDP port test functionality currently not available."
        <#
        $test = New-Object System.Net.Sockets.UdpClient;
        Try
        {
            Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
            $test.Connect($RemoteServer, $Port);
            Write-Host "Connection successful";
        }
        Catch
        {
            Write-Host "Connection failed";
        }
        Finally
        {
            $test.Dispose();
        }
        #>
    }
}

Test-Port -Protocol TCP -ComputerName MyComputer -Port 80
Test-Port -Protocol UDP -ComputerName MyComputer -Port 80

Test-Port -Protocol TCP -IP 208.67.222.222 -Port 53
Test-Port -Protocol UDP -IP 208.67.222.222 -Port 53

Reference: http://www.travisgan.com/2014/03/use-powershell-to-test-port.html

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code Network PowerShell

Post navigation

Previous post
Next post

Related Posts

PowerShell – Enable WinRM

Posted on September 5, 2017September 5, 2017

Not recommended for production computers but this will get the WinRM working on a development computer. Enable-PSRemoting -Force Set-WSManQuickConfig -SkipNetworkProfileCheck winrm set winrm/config/client/auth ‘@{Basic="true"}’ winrm set winrm/config/service/auth ‘@{Basic="true"}’ winrm set winrm/config/service ‘@{AllowUnencrypted="true"}’ Set-Item wsman:\localhost\client\trustedhosts * Winrm enumerate winrm/config/listener Restart-Service WinRM New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Name "Windows Remote Management…

Read More

Mac OS – Azure CLI Setup

Posted on September 15, 2017June 26, 2019

Download from Microsoft curl -L https://aka.ms/InstallAzureCli | bash echo "alias az=’$HOME/bin/az’" >> ~/.bash_profile exec -l $SHELL Install using HomeBrew brew update brew install azure-cli brew upgrade azure-cli Originally Posted on September 15, 2017Last Updated on June 26, 2019 All information on this site is shared with the intention to help….

Read More

Bash – Mass NSlookup

Posted on July 19, 2018

When you are moving from one server public IP to another you could use some help knowing things are changed. This bash script will do an NSLookup on various DNS servers. #!/bin/sh # Digkittell.net.sh # # # Created by David Kittell on 1/6/18. # netDomain="kittell.net" dnsServerList="68.94.156.132|10.40.20.1|8.8.8.8|192.168.10.21|192.168.10.20|10.40.20.6|208.67.220.220|9.9.9.9|209.18.47.61|4.2.2.2|168.63.129.16|172.26.38.1" clear echo "AT&T DNS…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes