

Microsoft has responded and in recent years, it has given its Windows systems with PowerShell a mature shell consisting of a command-line interpreter and a very extensive, object-oriented scripting language. With each new Windows version and with many updates of the operating systems and servers such as Exchange and the SQL server, new Cmdlets were added. Already with the appearance of Windows Server 2012 R2 and Windows 8.1, Microsoft had provided a whole series of new and extended Cmdlets for working around networks.

In today’s interconnected world, understanding and managing open ports on a Windows machine is not just about enhancing security; it’s a fundamental skill for ensuring efficient and secure communication between your computer and the network.
This article dives deep into how to find listening ports using two powerful tools: PowerShell and Netstat. Whether you’re a seasoned IT professional or a curious learner, mastering these tools can help you gain detailed insights into your computer’s network connections, troubleshoot issues, and secure your system against unauthorized access.
In this quick tutorial, we’ll show you how you can use PowerShell to quickly set a static IP address for your server or computer.
We’ve looked at PowerShell on many previous occasions on this blog, and as you’ll realise by now, it can be very convenient when managing operating systems, applications, services and even hardware.
One of the things you can do with PowerShell is query network settings and make changes to them, and that’s exactly what we’re going to do today.
NOTE: Please take great care when making changes remotely. When making changes to the network configuration, you will need to re-connect to the server once the changes have been applied. If you make a mistake, you might lose all access to the machine completely, unless you’re able to physically access the machine or use an iLO connection.

Using PowerShell to add a network printer to a computer can be quite efficient. Here’s a step-by-step tutorial:
- Open PowerShell as Administrator:
- Right-click on the Start button.
- Choose “Windows PowerShell (Admin)” or “Command Prompt (Admin).”
2. Check Existing Printers:
3. Add a Network Printer:
Use the Add-Printer cmdlet to add a network printer. You’ll need the printer’s IP address or hostname.
4. Set Default Printer (Optional):
If you want to set the added printer as the default printer, use:
Set-PrintDefault -Name "PrinterName"Replace “PrinterName” with the name of the printer you added.
5. Verify the Addition:
To confirm that the printer has been added, you can use:
That’s it! You’ve successfully added a network printer using PowerShell. Make sure to replace placeholder values with your actual printer information.
Introduction to Listening Ports and Their Importance
Listening ports are crucial gateways for data exchange between computers and other devices across networks and the internet. These are essentially digital doors through which a computer receives incoming requests and connections, making them fundamental to enabling network services and applications to communicate efficiently.
The Role of Listening Ports in Network Communication
Listening ports are designated numerical identifiers used by network protocols (such as TCP and UDP) to specify endpoints for communication on a host. Each service or application running on a computer listens for incoming connections on a specific port number. For instance, web servers typically listen on port 80 for HTTP traffic or port 443 for HTTPS traffic, allowing web browsers to connect and retrieve web pages.
The Importance of Monitoring Listening Ports
Monitoring ports is not just about ensuring smooth network operations; it’s also a critical component of a robust security posture. Malicious actors often scan ports looking for vulnerabilities to exploit, making unsecured or unnecessary open ports potential entry points for attacks.
- Identify Unauthorized Services: Uncover services that shouldn’t be running or that have been installed by malicious software.
- Enhance Network Security: Close or secure open ports that are not in use or that expose sensitive services to the internet, reducing the attack surface of a system.
- Troubleshoot Connectivity Issues: Resolve problems related to network access and service availability by ensuring that the correct ports are open and listening for the intended applications.
- Optimize Performance: Manage network resources more effectively by understanding traffic flow and service dependencies, leading to improved system and network performance.
Ah, Monday morning dramas — the boss has that “we’ve got a problem” look plastered on their face. Surprise, surprise! You’re moving offices, and guess who’s got to wave their IT magic wand to make your network play nice with the existing setup? Oh yes, it’s you! But hold on — before you start thinking of visiting each computer individually like it’s 1999, let’s introduce you to PowerShell. I present a PowerShell method for configuring static and DHCP IP addresses that utilizes the newer PowerShell cmdlets.
Newer versions of PowerShell mean more cmdlets
Microsoft implemented more PowerShell cmdlets, including some cmdlets that are specific to configuring network adapters and TCP/IP settings.
Using these commands, we can set various network configurations and settings more easily from a PowerShell script.
If you have multiple network adapters, you need to identify which network adapter you wish to view or modify by name, interface index, status, etc. For systems with one adapter, you can typically get this by using only Get-NetAdapter with no extra parameters. Please adjust this to suit your environment.
This cmdlet gets you the network configuration (interfaces, IP addresses, and DNS servers).
The Get-NetIPInterface cmdlet gets you the IP interfaces, which can include both IPv4 and IPv6 addresses and their associated configurations. We will use just IPv4 today, so my example reflects that.
The Set-NetIPInterface allows you to modify settings.
This cmdlet lets you set the DNS servers associated with a particular network interface.
This cmdlet lets you modify IP routes in the routing table, including wiping out all routes.
This cmdlet allows you to create and configure an IP address. This can include both IPv4 and IPv6 addresses.
How to set static/DHCP IP addresses
Enough of the boring explanations. Let’s move on to the fun!
We’re going to give a couple of examples for setting static and DHCP IP addresses by using the commands mentioned above.
Set a static IP address
Set an address with DHCP
How to disable static/DHCP IP addresses
In PowerShell, you can manage network adapter settings to disable or enable static (manually assigned) and DHCP (automatically assigned) IP addresses using the Set-NetIPInterface cmdlet. The specific command depends on whether you want to disable static or DHCP addresses. Here are the PowerShell commands for each scenario:
Disable a static IP address
To disable a static IP address on a network adapter, you can set the interface to obtain an IP address automatically (DHCP). Here’s the PowerShell command:
Replace “YourNetworkAdapterName” with the actual name or alias of your network adapter. This command configures the interface to obtain an IP address automatically from a DHCP server.
Disable DHCP (obtain IP automatically) and enable a static IP address
Replace “YourNetworkAdapterName” with the actual name or alias of your network adapter, “YourStaticIPAddress” with the desired static IP address you want to assign, “YourSubnetPrefixLength” with the subnet prefix length (e.g., 24 for a typical subnet mask of 255.255.255.0), and “YourDefaultGatewayIP” with the IP address of your default gateway.
Final thoughts
This is still just scratching the surface of what’s available. Remember to check out what properties and methods are available for Win32_NetworkAdapterConfiguration, and you’ll be able to customize all the settings for your particular environment. For a more complete list of cmdlets you can use to set network and TCP/IP settings, check out our PowerShell Commands Library.

Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet.
How to Set a Static IP Address Using PowerShell
Before you get started…
- To be registered with an organisation on the Jotelulu platform and to have logged in.
- A Servers subscription on the platform.
Setting a Static IP Address
First, you need to retrieve information about the current state of the network interface. To do this, run the “Get-NetIPConfiguration” command which will show the current network configuration.
- Description of the interface.
- Link Speed.
- MAC address or physical address.

Once you’ve done this, you’re ready to set a static IP address.
# New-NetIPAddress -IPAddress <IP_Address> -InterfaceAlias “<Interface_Name>” -DefaultGateway <Gateway_Address> -AddressFamily <IP_Type> -PrefixLength <Prefix_Length>
- <IP_Address> is the address you want to give the server using the format “X.X.X.X”.
- <Interface_Name> is what you want to name the interface.
- <Gateway_Address> is the gateway IP address using the format “X.X.X.X”.
- <IP_Type> specifies whether you’re using IPv4 or IPv6. For this tutorial, we’ll use IPv4.
- <Prefix_Length> should contain the number of bits for subnet mask, such as 24 for 255.255.255.0.
Here’s an example of what this command might look like:
# New-NetIPAddress -IPAddress 10.0.0.92 -InterfaceAlias “Ethernet” -DefaultGateway 10.0.0.254 -AddressFamily IPv4 -PrefixLength 24

Congratulations! You’ve set a static IP address for your server.
Using PowerShell for Advanced Port Analysis
Using Get-NetTCPConnection for Port Analysis
A primary cmdlet for analyzing TCP ports in PowerShell is Get-NetTCPConnection. This cmdlet provides detailed information about TCP connections, including the status of listening ports. Here’s how to use it:
List All TCP Connections Using PowerShell: Get-NetTCPConnection
Get-NetTCPConnection
To view all TCP connections, simply run Get-NetTCPConnection. This command returns a comprehensive list, including the local and remote addresses, port numbers, and the state of each connection.
Filtering Listening Ports: Get-NetTCPConnection -State Listen
Get-NetTCPConnection -State Listen
To narrow down the output to only those ports that are in a listening state, you can use Get-NetTCPConnection -State Listen. This command filters the results to show only the ports waiting for incoming connections, making it easier to identify what services are open to the network.
Examples and Syntax for Advanced Scenarios
Windows PowerShell flexibility allows for detailed analysis and custom reporting. Here are some examples demonstrating its capabilities:
Finding Processes Associated with Listening Ports:
Combining Get-NetTCPConnection with other cmdlets can reveal which process is associated with a particular listening port. For example, to find out which process is listening on port 443, you can use:
#
Get-NetTCPConnection -State Listen | Where-Object { $_.LocalPort -eq 443 } | ForEach-Object { Get-Process -Id $_.OwningProcess }
#This command sequence filters listening ports for port 443, and then retrieves the process associated with that port.
Listing Listening Ports for a Specific IP Address:
#
Get-NetTCPConnection -State Listen | Where-Object { $_.LocalAddress -eq "192.168.1.1" }
#Replace "192.168.1.1" with the IP address of interest.
Interpreting PowerShell Output
Understanding the output of the PowerShell command Get-NetTCPConnection is crucial for effective analysis:
- LocalAddress and LocalPort: These fields show the IP address and port number on the local machine that is involved in the connection. For listening ports, the
LocalAddressmight be0.0.0.0(listening on all interfaces) or a specific IP address. - RemoteAddress and RemotePort: For active connections, these fields show the address and port of the remote endpoint. For listening ports, these are typically not applicable.
- State: This field indicates the status of the port.
Listenmeans the port is open and waiting for incoming connections. Other states include Established active connections andClosedfor closed ports. - OwningProcess: The ID of the process that opened the port. This is particularly useful for identifying which application is responsible for a listening port.
Mastering Port Analysis: Empowering Your Network Management
Netstat offers simplicity and quick insights for those needing immediate information about active connections and listening ports. In contrast, PowerShell provides a deeper dive into network analysis, offering advanced filtering, scripting, and automation capabilities to handle complex network scenarios.
Summary
Thanks for choosing Jotelulu!
It Doesn’t Always Have To Be “ipconfig”
🤓 Get-Module -ListAvailable
🤓 Get-Command -Module NetTCPIP
🤓 Get-NetIPAddress
🤓 Get-NetIPAddress | Format-Table
Of course, it is also possible to reduce the output to just the addresses in the IPv4 range:
🤓 Get-NetIPAddress -Addressfamily IPv4 | Format-Table
🤓 Get-NetAdapter
🤓 Get-NetAdapter -Name “WLAN”
A good starting point for everything about PowerShell can be found on the Internet in the blog “Hey Scripting Guy!” (part of Microsoft TechNet). But there are simple scripts for beginners, as well as complex, more comprehensive solutions and examples.
Finding Listening Ports Using Netstat
Netstat, short for network statistics, is a command-line tool that comes with many operating systems, including Windows and is invaluable for any network diagnostics toolkit. It provides a way to display active connections, listening ports, routing tables, and a number of network interface statistics. Here, we will focus on how to use Netstat to find listening ports, a fundamental task for network management, security, and troubleshooting.
Basic Netstat Commands to Find Listening Ports
The power of Netstat lies in its simplicity and the breadth of information it can provide with just a few command-line arguments. To begin exploring listening ports with Netstat, let’s start with some basic commands:
List All Connections and Listening Ports: netstat -a
netstat -a
The command netstat -a displays all active connections and the TCP and UDP ports on which the computer is listening. This command provides a comprehensive overview but might include a lot of information to sift through.
Display Listening Ports Only: netstat -an | find “LISTENING”
netstat -an | find "LISTENING"
Show the Process ID Associated with Each Connection: netstat -ano
netstat -ano
Adding the -o option in netstat -ano will include the process ID (PID) in the output. This is particularly useful when you need to identify which process is associated with a specific listening port.
Examples and Syntax for Advanced Usage of Netstat
Display Only TCP Connections: netstat -at
netstat -at
Display Only UDP Ports: netstat -au
netstat -au
Similarly, for UDP ports, the command netstat -au lists active UDP connections. Because UDP is connectionless, the concept of “listening” doesn’t apply in the same way it does for TCP, but you can still see which UDP ports are open and ready to receive data.
Find a Specific Port Number: netstat -an | find “:80”
netstat -an | find ":80"
Viewing the Executable Involved in Creating the Connection:
While Netstat on Windows doesn’t directly show the name of the executable associated with each connection or listening port, you can use the PID provided by the -o switch and look up the PID in the Task Manager under the “Details” tab to find the corresponding executable name.
Interpreting Netstat Output
Understanding the output of Netstat is key to effectively using it for network diagnostics:
- Local Address: This shows the IP address of the local machine and the port number. A listening port will often show as
0.0.0.0:portor127.0.0.1:portfor TCP connections, indicating it’s listening on all interfaces or localhost, respectively. - Foreign Address: For listening ports, this is typically shown as
0.0.0.0:*or*:*, indicating the port is ready to accept connections from any IP address. - State: Indicates the state of the port. For TCP, this could be
LISTENING,ESTABLISHED,CLOSE_WAIT, etc. ALISTENINGstate means the port is open and waiting for incoming connections.
By leveraging these commands and understanding the output, you can effectively use Netstat to identify and analyze listening ports on your system, providing valuable insights for network management, security assessments, and troubleshooting efforts.



