How to set static & DHCP IP addresses in PowerShell

Jordan Hammond fun headshot
Jordan Hammond|August 29, 2023
Illustration of block with Powershell logo
Illustration of block with Powershell logo

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.

Because, really, who has time for tedious manual setups these days? Say goodbye to the netsh nightmares and hello to the dynamic duo: Get-NetAdapter and Set-NetIPInterface. Newer operating systems with updated versions of PowerShell come bearing gifts in the form of new cmdlets, transforming your network configurations into a breeze. From static IPs to DHCP, PowerShell swoops in to save the day — and your mood. So buckle up, sysadmins, because it's time to script your way to network bliss, leaving the manual headaches in the past where they belong.

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.

This cmdlet gets you the basic network adapter properties.

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.

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”}

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

$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

Set an address with DHCP

$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 }

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:

$interface | Set-NetIPInterface -InterfaceAlias "YourNetworkAdapterName" -Dhcp Enabled 

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

To disable DHCP and set a static IP address on a network adapter, you can use the following PowerShell commands:

$interface | Set-NetIPInterface -InterfaceAlias "YourNetworkAdapterName" -Dhcp Disabled $adapter | New-NetIPAddress -InterfaceAlias "YourNetworkAdapterName" -IPAddress "YourStaticIPAddress" -PrefixLength "YourSubnetPrefixLength" -DefaultGateway "YourDefaultGatewayIP" 

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 Hammond fun headshot
Jordan Hammond

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.

Related articles