Using PowerShell to Set Static and DHCP IP Addresses – Part 1

You come into the office on a Monday morning and your boss has an uncertain look on his face that reeks of trouble. You find out that you’re moving office suites and have to reconfigure your entire office network to play nicely with an existing network. A little part of you dies along with the hope of a productive week.

Until now, you may have been going to each computer individually to configure these settings manually. I feel your pain.

Perhaps you are more advanced and have experience using netsh. My hat’s off to you!

In either case, I present a PowerShell method for configuring static/DHCP IP addresses that utilizes the newer PowerShell cmdlets that were introduced with Windows 8. We’ll address older versions (including Windows 7) in another blog post in the near future.

Windows 8 and Newer Means More PowerShell cmdlets

Thanks to some new WMI classes, Microsoft implemented more PowerShell cmdlets that are only available to Windows 8 and above, including some cmdlets that are specific to configuring network adapters and TCP/IP settings (link here).

The commands that I’ll be using today are:

Using these commands, we can set various network configurations and settings more easily from a PowerShell script.

Get-NetAdapter

This cmdlet gets you the basic network adapter properties.

If you have multiple network adapters, you will need to identify which network adapter you wish to view/modify by name, interface index, status, etc. For systems with one adapter, you can typically get by with using only Get-NetAdapter with no extra parameters. Please adjust this to suit your environment.

As an example, on my test system, I have two network adapters. One is active and one is inactive. I grab only the active network adapter by using the following: Get-NetAdapter | ? {$_.Status -eq “up”}

Get-NetIPConfiguration

This cmdlet gets you the network configuration (interfaces, IP addresses and DNS servers).

Get-NetIPInterface / Set-NetIPInterface

The Get-NetIPInterface cmdlet gets you the IP interfaces, which can include both IPv4 and IPv6 addresses and their associated configurations. We will be using just IPv4 today, so my example screenshot reflects that. The Set-NetIPInterface allows you to modify those settings.

Set-DnsClientServerAddress

This cmdlet lets you set the DNS servers that are associated with a particular network interface.

Remove-NetRoute

This cmdlet lets you modify IP routes in the routing table, including the ability to wipe out all routes.

New-NetIPAddress

This cmdlet allows you to create and configure an IP address. This can include both IPv4 and IPv6 addresses.

Buy PDQ Deploy

Usage Examples

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.

Setting a Static IP Address – Windows 8 or Newer

$IP = "10.10.10.10"
$MaskBits = 24 # This means subnet mask = 255.255.255.0
$Gateway = "10.10.10.1"
$Dns = "10.10.10.100"
$IPType = "IPv4"
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
 $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
 $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
 # Configure the IP address and default gateway
$adapter | New-NetIPAddress `
 -AddressFamily $IPType `
 -IPAddress $IP `
 -PrefixLength $MaskBits `
 -DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS

Setting an address with DHCP – Windows 8 or newer

$IPType = "IPv4"
$adapter = Get-NetAdapter | ? {$_.Status -eq "up"}
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
 # Remove existing gateway
 If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
 $interface | Remove-NetRoute -Confirm:$false
 }
 # Enable DHCP
 $interface | Set-NetIPInterface -DHCP Enabled
 # Configure the DNS Servers automatically
 $interface | Set-DnsClientServerAddress -ResetServerAddresses
}

In Conclusion

This post is geared for those of us that have machines that are Windows 8+ in our environments and can make use of these new cmdlets.

This barely scratches the surface for what’s available with the newer cmdlets that you can use with Windows 8 and newer operating systems. For a more complete list of cmdlets that you can use to set network and TCP/IP settings, here’s a link to the technet page – link.

Tune in for part two of this blog series when I will show how to set IP address configuration by using Get-WMIObject. This will allow you to set your IP address settings in versions of Windows prior to Windows 8.

Go on to Part II of this blog, click here.

Buy PDQ Inventory