- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
PowerShell includes a few cmdlets that allow us to find a machine's IP address and change it to any configuration we'd like. Let's go through a few scenarios.
Setting a static IP address to DHCP
One of the easiest tasks to perform is to set a NIC to DHCP. Using the Set-NetIPInterface and Remove-NetRoute cmdlets, we can make it happen. We're starting out with an IP configuration that looks like the one below.
To set this NIC to DHCP, we can use the Set-NetIpInterface command. This will remove the IP address and subnet mask.
Set-NetIPInterface -InterfaceAlias 'Ethernet 2' -Dhcp Enabled
Upon further inspection though, the default gateway is grayed out in TCP/IP properties but still remains. We have to use a different command to remove this, called Remove-NetRoute.
PS> Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute Confirm Are you sure you want to perform this action? Performing operation "Remove" on Target "NetRoute -DestinationPrefix 169.254.114.202/32 -InterfaceIndex 8 -NextHop 0.0.0.0 -Store Active" [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): a
This sets the NIC to DHCP with no remnants of the previous default gateway.
Setting a static IP address from DHCP
When going from DHCP to static, the PowerShell cmdlets treat this as a "new" IP address, thus the use of the New-NetIpAddresscmdlet. To use this, you'll need to reference the current IP address and pipe it to New-NetIpAddress using the expected IP address, subnet mask prefix length, and default gateway.
PS C:> Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress IpAddress 192.168.1.10 -PrefixLength 24 -DefaultGateway 192.168.1.1 IPAddress : 192.168.1.10 InterfaceIndex : 8 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.1.10 InterfaceIndex : 8 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
Changing a static IP address
To change an existing static IP address, we have to use the Set-NetIpAddress cmdlet, but it's not quite that easy. To change an existing static IP address, you should first removethe existing one and create a new one. I'll remove the current IP address and remove the default gateway.
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetIpAddress
Once I do this, I'll then use the same technique I used above to add a new IP address.
Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress ‑IpAddress 192.168.1.11 -PrefixLength 24 -DefaultGateway 192.168.1.1
You should now see that the IP address has changed.
Subscribe to 4sysops newsletter!
PS C:\> Get-NetIpAddress -InterfaceAlias 'Ethernet 2' IPAddress : 192.168.1.11 InterfaceIndex : 8 InterfaceAlias : Ethernet 2 <SNIP>
Summary
Changing a NIC IP address and default gateway isn't quite as cut and dry as you'd expect. Due to the way Windows stores IP information, you'll find that completely removing the old IP and default gateway and simply assigning new ones via the cmdlets is the easiest way to accomplish this task.
This is a nicely written blog and it seems like it may confirm my suspicion that it is not possible to programmatically with powershell determine whether or not a client's DNS server is set to manual or obtain via DHCP. You can only retrieve the DNS servers that are set, but you can't tell whether they were assigned manually or via DHCP.
I am looking for a way in powershell to determine if the machine has any static settings at all in any of the LAN adapters. Ideas on how to do this would be appreciated.
Nice hint there Rick. It took me a while 🙂 It really seems there is no straight-forward way, not even with WMI. If there is DHCP=Enabled on adapter, you can still have a static DNS. The only way I was able to find out is to go thru records in following Registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\
If any of them has value in key NameServer (REG_SZ) this is manually assigned DNS. For DHCP assigned DNS its key DhcpNameServer.
So you can do a simple script to list adapters where DHCP=Enabled, take this adapter IP and look for Registry path where DhcpIPAddress = yourAdapterIP and check the NameServer or DhcpNameServer values.
This will be a simple script providing you requested results. Hope this helps, atm I dont have any server around to type the script.
Alternatively you can use
and parse the output looking for "Statically Configured…". Tho I guess the Registry way is much easier .)
In this bit of code, you forgot the dash in front of "IpAddress 192.168.1.10;" that might throw some folks off. Thanks for the write-up!
How would you do this while invoke-command to a VM? without causing networking to drop?
From the logic point of view, you cant change IP address without a network outage. You can invoke command to a VM even without networking from the Hyper-V host, see https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/powershell-direct
Thank you for adding the part about removing the existing address, of the countless articles I found about changing the IP via Powershell, you’re the only one who mentioned anything about removing the existing address. I was going in circles until I found your article.
this reg step has to be done or you will get error ” New-NetIpAddress : Inconsistent parameters PolicyStore PersistentStore and Dhcp Enabled ” when you try to set staticIP address