PowerShell How-To

How To Set an IP Address with PowerShell

Changing network adapter properties can be a chore the more machines you have, and the more numbers you have to punch in. Script out the process, instead.

A common practice that many IT professionals are used to is changing network adapter properties in Windows. Just about everyone has had the opportunity to right-click on the network adapter, go to Properties, go to the TCP/IP properties, and change the IP address, DNS server, default gateway and so on.

This gets it done -- but not if you need to do it across lots of machines at once or ensure you don't fat-finger a number. Let's instead script this out in PowerShell. (Note: For the remainder of this article, I'm going to be assuming you have PowerShell v5 installed on a Windows 10 client.)

To set an IP address on a network adapter in Windows, we have the New-NetIPAddress command. This command is part of the NetTcpIp module and is included with PowerShell v5 and later. But, before we change something, we should always check the current configuration. To get the current IP address, we'll use Get-NetIPAddress.

When you run Get-NetIpAddress, you'll probably be presented with something like the below. Even if you have one NIC in your network adapter list, you'll be presented with more adapter than what you expect. You'll see NICs bound for IPv6, IPv4, loopback adapters, VPN adapters and more. The first thing we need to do is figure out the adapter we'd like to change.

PS> Get-NetIPAddress

IPAddress         : fe80::b0d8:365f:2bec:fdbf%10
InterfaceIndex    : 10
InterfaceAlias    : Ethernet 3
AddressFamily     : IPv6
Type              : Unicast
PrefixLength      : 64
PrefixOrigin      : WellKnown
SuffixOrigin      : Link
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : fe80::100:7f:fffe%20
InterfaceIndex    : 20
InterfaceAlias    : Local Area Connection* 9
AddressFamily     : IPv6
Type              : Unicast
PrefixLength      : 64
PrefixOrigin      : WellKnown
SuffixOrigin      : Link
AddressState      : Deprecated
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : ::1
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv6
Type              : Unicast
PrefixLength      : 128
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 10.3.222.187
InterfaceIndex    : 10
InterfaceAlias    : Ethernet 3
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 32
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.86.91
InterfaceIndex    : 6
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 22:36:23
PreferredLifetime : 22:36:23
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 127.0.0.1
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 8
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

Looking at the parameters available, I can limit this list down by IPv4 since I don't want to change my IPv6 address. I'll use the AddressFamily parameter and specify IPv4 to do that.

PS> Get-NetIPAddress -AddressFamily IPv4

IPAddress         : 10.3.222.187
InterfaceIndex    : 10
InterfaceAlias    : Ethernet 3
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 32
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.86.91
InterfaceIndex    : 6
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 22:32:07
PreferredLifetime : 22:32:07
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 127.0.0.1
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 8
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

I'm now down to three adapters from six. I then notice the InterfaceAlias property and match that up to the network adapter list. I can see that I need to change the Ethernet 2 alias. I'll specify this directly, and I now get the adapter I'm looking for.

PS> Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias 'Ethernet 2'

IPAddress         : 192.168.86.91
InterfaceIndex    : 6
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 22:28:41
PreferredLifetime : 22:28:41
SkipAsSource      : False
PolicyStore       : ActiveStore

Now that I've got the adapter in my sights, I can now change the IP address using New-NetIPAddress. New-NetIpAddress has a few parameters that you'll need to change the IP, like the actual IP, the subnet mask (CIDR notation) and the default gateway. All of these parameters can be passed to New-NetIPAddress like below:

PS> New-NetIPAddress –InterfaceAlias 'Ethernet 2' –IPv4Address '192.168.86.92' –PrefixLength 24 -DefaultGateway '192.168.86.1'

IPAddress         : 192.168.86.92
InterfaceIndex    : 6
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Tentative
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 192.168.86.92
InterfaceIndex    : 6
InterfaceAlias    : Ethernet 2
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Manual
SuffixOrigin      : Manual
AddressState      : Invalid
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : PersistentStore

If you check the network adapter properties via the GUI now, you'll see that your IP address has been changed!

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular