The second post in my PowerShell Beginner series, “Daily Tasks, The PowerShell Way“. Learn how to get network connection information and configure a static IP or DHCP using PowerShell.
Are you connected?
In my previous post we covered how to use Test-NetConnection for network connectivity troubleshooting. If you connected to a DHCP enabled network, you will likely have successful tests without any action on your part. However, if your network environment requires static configuration of IP addresses and DNS servers for servers and/or workstations, you need to configure the correct network settings for a newly provisioned computer.
Get network connection details
First, let’s see what’s connected. Get-NetAdapter will return basic network information. We see network connection “Ethernet0” is up with a speed of 1Gbps.
PS C:\Windows\system32> Get-NetAdapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet0 Intel(R) 82574L Gigabit Network Conn... 5 Up 00-0C-29-01-81-15 1 Gbps
We can use the cmdlet Get-NetIPInterface to get a summary table of our network interfaces and their connection state. Because I’m only concerned about IPv4, I include the parameter -AddressFamily IPv4. We can see that I have one physical interface connected “Ethernet0”, and it is currently configured for DHCP.
PS C:\Windows\system32> Get-NetIPInterface -AddressFamily IPv4 ifIndex InterfaceAlias AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp ConnectionState PolicyStore ------- -------------- ------------- ------------ --------------- ---- --------------- ----------- 5 Ethernet0 IPv4 1500 10 Enabled Connected ActiveStore 1 Loopback Pseudo-Interface 1 IPv4 4294967295 50 Disabled Connected ActiveStore
We can use the Get-NetIPConfiguration cmdlet to return IP information for “Ethernet0”. We learn the IPv4 address, the default gateway, and DNS servers. One thing that is absent from this result set is subnet mask, hopefully that will be added in the future. However, we can get the subnet mask as a prefix value if we run Get-NetIPAddress. You can see PrefixLength is 24 (/24 or 255.255.255.0).
PS C:\Windows\system32> Get-NetIPConfiguration -InterfaceAlias "Ethernet0" InterfaceAlias : Ethernet0 InterfaceIndex : 5 InterfaceDescription : Intel(R) 82574L Gigabit Network Connection NetProfile.Name : Network 2 IPv4Address : 192.168.1.130 IPv6DefaultGateway : IPv4DefaultGateway : 192.168.1.2 DNSServer : 192.168.1.2 PS C:\Windows\system32> Get-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4 IPAddress : 192.168.1.130 InterfaceIndex : 5 InterfaceAlias : Ethernet0 AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Dhcp SuffixOrigin : Dhcp AddressState : Preferred ValidLifetime : 00:16:19 PreferredLifetime : 00:16:19 SkipAsSource : False PolicyStore : ActiveStore
Set static IP address
You may want to set a static IP if your computer is a server or need to set static IPs in an environment without DHCP. You can do this using the New-NetIPAddress cmdlet. This is equivalent to opening the Properties of a network connection and manually configuring the adapter.
PS C:\Windows\system32> New-NetIPAddress -InterfaceAlias "Ethernet0" -AddressFamily IPv4 -IPAddress 192.168.1.230 -PrefixLength 24 -DefaultGateway 192.168.1.2 IPAddress : 192.168.1.230 InterfaceIndex : 5 InterfaceAlias : Ethernet0 AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Manual SuffixOrigin : Manual AddressState : Tentative ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore
Set DNS servers manually
If you configure a static IP address, you will also need to manually configure DNS servers for the connection. Use Set-DnsClientServerAddress. You can specify multiple DNS servers separated by commas.
PS C:\Windows\system32> Set-DnsClientServerAddress -InterfaceAlias Ethernet0 -ServerAddresses 192.168.1.2,8.8.8.8,8.8.4.4 PS C:\Windows\system32>
If you manually inspect the adapter, you can verify the static IP information and DNS servers are set.
Reset network connection to DHCP
My demo computer is working with a static IP configuration, but now that I’m done I want to revert it back to its original DHCP configuration. To do that in PowerShell, I need to do three things:
- Set the connection back to “Obtain an IP address automatically”
- Remove the static default gateway
- Set the connection back to “Obtain DNS server address automatically”
I can do this by running a sequence of three cmdlets, Set-NetIPInterface, Remove-NetRoute, and Set-DNSClientServerAddress. I will also incorporate Get-NetIPInterface and use piping to streamline the command input.
# Return network interface to a variable for future use $interface = Get-NetIPInterface -InterfaceAlias "Ethernet0" -AddressFamily IPv4 # Remove the static default gateway $interface | Remove-NetRoute -AddressFamily IPv4 -Confirm:$false # Set interface to "Obtain an IP address automatically" $interface | Set-NetIPInterface -Dhcp Enabled # Set interface to "Obtain DNS server address automatically" $interface | Set-DnsClientServerAddress -ResetServerAddresses
Next steps
- The next time you are provisioning a server or workstation, use PowerShell to configure a static IP and DNS servers.
- If the computer ultimately should use DHCP, use PowerShell to remove the static configuration and revert to DHCP.
Reference
- technet.microsoft.com