Solved: powershell see ports in use

Sure, let’s start.

Powershell, a powerful task automation and configuration management framework from Microsoft significantly eases the job of a system administrator. With the advent of the Powershell scripting language along with its cmdlets, users are provided with rich access to a data store. One such use of PowerShell is to check the ports in use on a system. Ports are communication endpoints for a machine, and each port has a specified port number.

# PowerShell Script to list all the TCP/IP ports which are being used on your local system

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 

Function Test-PortInUse {   
  Param(
    [Parameter(Mandatory = $true)]
    [int]$StartPort,
    [Parameter(Mandatory = $true)]
    [int]$EndPort
  )
  ForEach ($i in ($StartPort..$EndPort)){
    $connection = Test-NetConnection -ComputerName LocalHost -Port $i
    if ($connection.TcpTestSucceeded){
       $i
    }
  }
}

# This will test all ports between 20 and 25
Test-PortInUse 20 25

The script operates within a function, Test-PortInUse, which accepts two parameters, a starting port number, and an ending port number. The function loops ($StartPort..$EndPort) between the provided ranges of ports, for each port number it then runs the Test-NetConnection cmdlet on the localhost for that port.

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser – This command is required to run PowerShell scripts, and it changes the user preference for the PowerShell execution policy.

The Test-NetConnection cmdlet displays diagnostic information for a connection. Taking as parameters LocalHost – which refers to the local machine and Port – indicating which port to test; checks if a connection can be made to that port.

Powershell Scripting

Powershell Scripting is a versatile platform developed by Microsoft for its Windows environment. This dynamic language provides an array of utilities right out of the box. One such utility we make use of is the Test-NetConnection cmdlet. This utility offers a simple way to perform a variety of network tests that can be scripted and automated, perfect for administrators managing large networks.

The Importance of Port Management

Port management is an essential task for IT administrators, DevOps, and security professionals as open and vulnerable ports can be potential areas of risk. Being able to view, monitor, test, and close ports on demand becomes an important, if not essential, skill to have. PowerShell scripts can play a pivotal role in automating these tasks and PowerShell’s cmdlets streamline the process.

How the Test-NetConnection cmdlet Works

Test-NetConnection is a cmdlet that renders a deep scriptable cmdlet interface to numerous network diagnostics interface exposed by operating systems. The cmdlet especially shines when it comes to testing a specific service within a server, or to test the networking latency of the connection between the source and the destination, whether it’s TCP or ICMP.

The information provided by this cmdlet is particularly useful when trying to identify open ports and view the types of connections that a machine has with a network.

In conclusion, Powershell, along with its various cmdlets like Test-NetConnection, provides various capabilities that greatly aid in network, port management, allowing for efficient and effective automation and management of such tasks.

Related posts:

Leave a Comment