Update: Learnt from this StackOverflow post that the cmdlets below are only available on PowerShell v3 running on Windows 8/ Windows Server 2012 (and later). They are not available on PowerShell v3 running on Windows 7/ Windows Server 2008.
Windows Server 2012 comes with PowerShell v3 and that has the ability to manipulate the network interface from within PowerShell. Meaning you can view the IP address, set IP address, change name of the interface, and so on. I find that cool!
I explored these new cmdlets by typing
Get-Net at the PowerShell prompt and pressing
TAB. This shows all the commands and I kept trying the ones I felt interested in and discovered new ones from reading the help pages.
Get-NetIPInterface shows you the available network interfaces. You can pass the cmdlet parameters to filter the results in terms of (say) showing only the interfaces that are connected or showing only the interfaces that are assigned an IP from DHCP.
To see the IP addresses, use the
We can combine the two cmdlets. For instance, to find the IP addresses of the DHCP assigned interfaces one can pipe the two commands:
Get-NetIPInterface –Dhcp Enabled | Get-NetIPAddress
To change an interface settings such as enable/ disable DHCP use the
To assign a new IP address use the
New-NetIPAddress cmdlet (this automatically disables DHCP on that interface if it’s enabled). To change the property of an existing IP address (such as the subnet mask, for instance) use the
Set-NetIPAddress cmdlet. And To un-assign an IP address use the
I find the
Set-NetIPAddress cmdlet slightly confusing. One would expect it to be able to set an IP address too, for instance, but it does not work that way. To add to the confusion this cmdlet too has switches similar to the
New-NetIPAddress cmdlet to specify an IP Address (the
-IPAddress switch) so you’d think it’s possible to set an IP address this way. But don’t be fooled. All this
–IPAddress switch does with the
Set-NetIPAddress cmdlet is to let you select interfaces matching that IP address.
If you try and set an IP address using the
Set-NetIPAddress cmdlet you get an error:
The error message is obvious. You can see the cmdlet is trying to find interfaces matching the IP address you specify – and failing – rather than set that as the IP address of an interface.
Moving on, I like to rename the network adapters in my VMs. That too is possible using PowerShell now.
Rename-NetAdapter is your friend!
I also like to disable some of my network adapters. You can do that too now through PowerShell using the
Would have been handy if cmdlets such as
Disable-NetAdapter were a part of
Set-NetAdapter (via a switch).
PowerShell is also now cool enough to fiddle with the bindings. So, for instance, if you want to disable IPv6 for one of your interfaces – possible now via PowerShell! Use
Get-NetAdapterBinding to see the available bindings (IPv4, IPv6, etc) and disable using
Goes without saying – all these
Disable-* cmdlets have an
Enable-* counterpart too. So you can enable whatever you disable.
This TechNet topic gives a list of all the Network related cmdlets in PowerShell.
When assigning a new IP address to an interface using
New-NetIPAddress you can pass a default gateway IP too via the
–DefaultGateway switch. If you forget to do that, there’s no way to add a default gateway – perhaps using the
Set-NetIPAddress cmdlet as I was expecting. The only alternatives are to remove the IP address via the
Remove-NetIPAddress cmdlet and then re-add the IP address but this time specifying the default gateway; or use the
New-NetRoute cmdlet to manipulate the routing table directly.
Get-NetRoute cmdlet can be used to view the existing routing table. And the
New-NetRoute can be used to add a new route. To make a route the default gateway set the destination prefix as
0.0.0.0/0 (for IPv4) or
::/0 (for IPv6). Examples below:
Lastly, there are cmdlets to configure the DNS resolvers. The
Get-DnsClient cmdlet shows you DNS configuration information for each interface. It doesn’t show the resolver addresses; rather this cmdlet is about the DNS client itself and so shows information such as the DNS suffixes and the search list for these suffixes. The
Get-DnsClientServerAddress cmdlet does what it says – it shows you the resolver server address for each interface – and is probably what most of us will commonly use.
To set DNS resolves you can use the
Set-DnsClientServerAddress cmdlet. To specify multiple addresses put them in brackets with the entries comma separated and in double quotes. The double quotes are important because without them the addresses are ignored.
The same cmdlet can be used with a
–ResetServerAddresses switch to remove the server addresses.
And that’s more or less it. These cmdlets only touch the tip of the ice-berg, but I think these are the ones most of us will regularly use.
Just to summarize here’s a table with all the cmdlets:
||Shows you the available network interfaces. Can pass parameters to filter the results (e.g. only the DHCP assigned ones).|
||Shows you the IP addresses. Again, can filter using parameters.|
||Change an interface settings. Such as turn off/ on DHCP, IPv6 neighbor discovery settings, router settings (advertising, packet forwarding), and Wake on LAN.|
||Assign a new IP address to an interface. Use the –DefaultGateway switch to specify the default gateway.|
||Remove an assigned IP address from an interface.|
||Change IP address properties. For instance: change the subnet mask.|
||Rename a network adapter.|
||Disable a network adapter. To enable use
||View the network adapter bindings. Such as IPv4, IPv6, Client for Microsoft Networks.|
||Disable network adapter bindings. To enable use
||View the routing table.|
||Add an entry to the routing table. Use destination prefix as
||Remove a routing table entry.|
||View the DNS client settings. Such as DNS suffix, search list, and so on.|
||View the DNS client server addresses.|
||Modify the DNS client settings.|
||Add DNS client server addresses. Put multiple address as
Now that’s a good reference for me too to check whenever I forget these commands!