- 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
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.
PS C:\> Get-NetIpAddress -InterfaceAlias 'Ethernet 2'
IPAddress : 192.168.1.11
InterfaceIndex : 8
InterfaceAlias : Ethernet 2
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.