Work with Network Adapters in command line (Part 1)
Let’s do this with powershell!
Trust me on this, when you need to change the IP address on multiple devices, it can quickly become a nightmare. Fortunately, there is an easy way to work with the Windows network adapters with Powershell. Let’s take a quick look at the most useful commands :
Get the current IP configuration
Ok, we all know the ipconfig command, but it wouldn’t be fun to talk about that one right? It’s Powershell time baby!
Get-NetIPConfiguration

So now you might be saying: Ok, but this doesn’t give close enough information compared to the old ipconfig right?
You’re right, but like you had to do ipconfig /all to get more information, you need to add an option to this cmdlet to get more information out of it :
Get-NetIPConfiguration -Detailed

There you go! Lots more information here! Let’s compare with the good old Ipconfig /all

See? We mostly get the same information, but with a few additions like the Network Profile used and the MTU. If those two pieces of information don’t give you enough benefits to move over Powershell then maybe my next points will convince you!
Work with the Network Adapter IP Configuration
So, we’ve seen how we can get the IP configuration from Powershell. Now let’s see how we can set a static IP address on that computer with a simple command.
New-NetIPAddress -InterfaceAlias <Interface Alias> -IPAddress <New IP> -PrefixLength <Prefix length> -DefaultGateway <Gateway>

So seems like it worked right? It did, but something is missing…

Wait! Where are my DNS IP addresses? They’re gone! Looking in the GUI, we can quickly see what happened :

So the thing is that whenever you set a static IP address, it automatically wants you to set your DNS manually as well. As you can see, they even greyed out the option to set automatically. Of course, there’s a way to do this with Powershell as well!
Set-DnsClientServerAddress -InterfaceAlias <Interface Alias> -ServerAddresses <DNS IP Addresses in array format>

There you go, DNS is set, IP is Fixed and sure enough, if we look in the GUI, we get the same result!

Ok, we had our fun, let’s put this interface back to DHCP :
Set-NetIPInterface -InterfaceAlias <Interface Alias> -Dhcp Enabled

It worked! Kind of… So the thing with Powershell, is that once you put it back to DHCP, it won’t request for a new lease. Also, you might be noting that the DNS is still fixed. The reason for that is that while you can’t put DNS on DHCP when you set a static IP, you can set your DNS to static when you have your IP on DHCP. So let’s fix both issues. Unfortunately, there is not (yet) a built-in command in PowerShell to ask for a new DHCP lease, so we will need to rely on IPConfig /renew this time to fix this.

As for the DNS, a quick Powershell command will fix that for us
Set-DnsClientServerAddress -InterfaceAlias <Interface Alias> -ResetServerAddresses

Why relying on Powershell with a simple example
So let’s put this into practice with a simple example. Let’s do something we couldn’t do without Powershell!
Let’s say we want to convert the current DHCP address of a workstation to a Static IP, but with the same IP configuration, it already has. Why do you say? Well, you might need to do this if you want to migrate a Windows DHCP Server over a device that doesn’t support DHCP reservations outside of its DHCP scope (which you can do on a Windows DHCP server, but can’t on a Fortigate for example).
So you would need to either do all Windows devices one by one, connecting to them, and set the IP manually OR you could run this script on it.
$InterfaceAlias = "Ethernet" # Interface Alias
$AddressFamily = 2 # Specify either 2 for IPv4 or 23 for IPv6
#Here we get the IP configuration. Because we also need the Prefix Length, I will use Get-NetIPAddress
$CurrentIPConfig = Get-NetIPAddress -InterfaceAlias $InterfaceAlias | Where-Object {$_.AddressFamily -eq 2 }
#The fun thing about Get-NetIPConfiguration is that the IPv4DefaultGateway refer to a route. So I need to grab the next hop on that route
$CurrentIPConfigGateway = (Get-NetIPConfiguration | Foreach IPv4DefaultGateway | Where-Object {$_.ifIndex -eq (Get-NetIPConfiguration -InterfaceAlias $InterfaceAlias).InterfaceIndex }).NextHop
#Here, we get the DNS servers IP addresses
$CurrentDNSConfig = Get-DnsClientServerAddress -InterfaceAlias $InterfaceAlias | Where-Object {$_.AddressFamily -eq 2 }
#Let's push our configuration to the Ethernet adapter
Remove-NetIPAddress -InterfaceAlias $InterfaceAlias -IPAddress $CurrentIPConfig.IPAddress -Confirm:$false #Because we are setting the same IP, both (DHCP and Fixed) can't exist at the same time, so we need to remove it first
New-NetIPAddress -InterfaceAlias $InterfaceAlias -IPAddress $CurrentIPConfig.IPAddress -DefaultGateway $CurrentIPConfigGateway -PrefixLength $CurrentIPConfig.PrefixLength
Set-DnsClientServerAddress -InterfaceAlias $InterfaceAlias -ServerAddresses $CurrentDNSConfig.ServerAddresses
From there, you could adapt the script so it could grab the right interface automatically (assuming not all devices have the alias “Ethernet”). You could also run it remotely with a GPO login script or any RMM tool of your choice.
You see, the possibility are endless here.
[…] my last article (Work with Network Adapters in command line (Part 1)), we saw how to work with the IP configuration using Powershell. For multiple reasons, I prefer […]