PowerShell for Network Engineers

Tesla Coil

In my last PowerShell post, I hope I convinced you that PowerShell isn’t just for the server engineers. This time we’ll take a look at a few more cmdlets that can come in handy for network engineers.

A Ping of a Different Color

Ok so ping is not exactly revolutionary, but if you’re wanting to script a bunch of pings, PowerShell has you covered. In PowerShell, a ping is accomplished using the cmdlet Test-Connection. At its simplest form, it’s just like a ping command.

Test-Connection -ComputerName srv01

Of course, there’s no need to do that in PowerShell, but it can be useful for scripting. By adding the “-Quiet” parameter the results of Test-Connection becomes a simple boolean of true or false. This can then be used with if statements to only try to run an additional cmdlet if the Test-Connection succeeds.

Another way to use Test-Connection in scripting is to have it help you ping multiple computers. One way is to list multiple hosts after the “-ComputerName” parameter separated by commas. Programatically it may also make sense to do something like the following example.

For ($i=1; $i -le 255; $i++) {
    Test-Connection -ComputerName 192.168.1.$i

That code will iterate through IP addresses pinging each, in turn, starting at and ending with The next step is to make the cmdlet ping a destination from multiple hosts.

Test-Connection -Source srv01, srv02, srv03 -ComputerName otherSrv01

More about the Test-Connection cmdlet and the various data forms that it can return can be found at Microsoft’s documentation site.

What’s Your Address?

Another common task is to retrieve and/or set the IP address for a network interface. PowerShell does not disappoint and provides the cmdlets for the job. The Get-NetIPConfiguration cmdlet will list the IP Address, interface, default gateway, and DNS servers.


InterfaceAlias       : Ethernet
InterfaceIndex       : 1
InterfaceDescription : Intel(R) Ethernet Connection (2) I219-LM
NetProfile.Name      : packitforwarding.com  2
IPv4Address          :
IPv4DefaultGateway   :
DNSServer            :

You may have noticed that there was one thing missing that you may want to know about. What is the subnet mask? Well at least at the time I’m writing this, there is not a native PowerShell way to get the subnet mask, but you can get the prefix length. The Get-NetIPAddress cmdlet will get the prefix and it also will add information about the current remaining life of the DHCP lease as an added bonus.


IPAddress         :
InterfaceIndex    : 1
InterfaceAlias    : Ethernet 
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 5.17:21:00
PreferredLifetime : 5.17:21:00
SkipAsSource      : False
PolicyStore       : ActiveStore

There are two cmdlets for setting the IP address. The first one Set-NetIPAddress allows you to enable or disable DHCP. The second one New-NetIPAddress allows you to set a static IP address.

New-NetIPAddress -InterfaceIndex 1 -IPAddress -PrefixLength 24 -DefaultGateway

Set-NetIPAddress -InterfaceIndex 1 -DHCP Enabled

While you will probably never have a need to use a lot of these cmdlets on a one-off situation, their power comes from the ability to use them as part of a larger script. In one case I was able to use the Set-NetIPAddress to enable DHCP for a set of computers so that they would migrate from static IPs to DHCP without needing to touch each individually. This allowed me to migrate an entire floor (about 100 computers) in one night without stepping into the building. That’s the power of automation!