Know your network with PowerShell and TCP

 Published by Patrick Gebhardt
Last updated on January 23, 2024 • 8 minute read

What does an administrator do when they need quick information about their network and their TCP / IP configuration? They use a command like "ipconfig", "netsh" or "ping" in the command prompt. But not only the fact that Microsoft has long questioned the availability of the "netsh" command, but also the various possibilities of current technologies should prompt IT professionals to take a look at PowerShell and its network commands.

know your network with powershell and tcp

Powershell

 

The command line - traditionally referred to as a command prompt on Windows systems - has always been the hallmark of "real IT professionals." Especially the eager Linux advocates have looked down on "Click-Happy" users for years, who supposedly would only find what they want on their systems at all with the help of the Windows interface.

As usual in such quasi-religious disputes, there were also many truths on both sides, along with many exaggerations and animosities: for "normal" users (and also for many IT pros), working with the sometimes very cumbersome shell commands was more than exhausting. And on the other hand, many Windows professionals missed the flexibility and programmability of the shell scripts when, for example, they wanted to automate network and system admin tasks.

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.

Even then, Microsoft offered approximately 250 Cmdlets (PowerShell version 3.0). This development has continued with Windows 10 and Windows Server 2016. On systems running Windows 10 and Windows Server 2016, Version 5 of the PowerShell is currently available for administrators.

The Command:

🤓 Get-Module -ListAvailable

... brings a large number of modules to the screen, many of which are labeled "Net", and therefore are of particular interest to the administrator when they need information about their network and corresponding hardware, such as the network adapter. Another command of this type:

🤓 Get-Command -Module NetTCPIP

... then shows again which commands are available within the NetTCPIP module. System administrators who need information about their network will usually use the "ipconfig" command - which can also be started directly from the PowerShell command line. Similar to "netsh", however, the user often has to torment himself by going through various submenus before he gets the desired information. 

Selecting the corresponding PowerShell Cmdlet makes it much easier. If you need information on the IP address, you can get it with the following command:

🤓 Get-NetIPAddress

... It will then display both the configuration information for the IPv4 and the IPv6 addresses for all network adapters in the system. On a server system with multiple network cards, many entries are displayed. Those who are then overwhelmed by the wealth of information displayed can filter the output accordingly. If only the basic information is needed, usually the Format-Table Cmdlet (to which the output is passed by means of a pipeline) helps:

🤓 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

... A very useful Cmdlet also displays all the network interfaces installed on the computer directly:

🤓 Get-NetAdapter

... Again, it is easily possible to filter the output further. For example, when it comes to listing only the existing WLAN interfaces:

🤓 Get-NetAdapter -Name "WLAN"

... The commands and thus also the gained information are truly flexible due to the fact that the PowerShell commands can be linked by means of a pipeline (as already briefly shown with Format-Table). What about, for example, displaying only the IP addresses in the system that were assigned via DHCP? To do so, we first use the Get-NetIPAddress Cmdlet, which displays the existing IP addresses. This output is passed to "Where-Object". There, the addresses that were assigned by DHCP are then filtered out by comparing the prefix (-eq stands for "equal"). Once again forwarded to "Select-Object", then only the name and the IP address of the found interfaces are displayed:

 

Get-NetIPAddress |
  Where-Object PrefixOrigin -eq dhcp |
  Select-Object -Property IPAddress, InterFaceAlias

 

Even these few examples clearly show the potential in PowerShell network commands, which can also be used remotely on other Windows systems. System managers and administrators should familiarize themselves with these possibilities, and in this way, create their own library with scripts for network support.

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.