Managing Network Settings with PowerShell 7
Built-in PowerShell 7 module
NetTCPIP, on Windows, offers a range of functionality to update and manage your network interfaces. Many of the traditional IT administration tasks, such as setting a static IP address or DNS Server settings, are trivial using the functions within the
Let’s explore a typical series of steps that many an IT administrator might face. Setting up a new server and it’s associated network interface.
Discovering Network Interfaces
Perhaps you’ve been tasked with setting up a brand new virtual machine (VM) and configuring its network interface. Rather than go through the GUI, you know PowerShell is already installed on the latest version and ready to go. After opening up a new console session, the first thing we need to do is see what our network interfaces currently are.
Say Goodbye to Traditional PC Lifecycle Management
Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.
Get-NetAdapter, will display every network interface that the system is aware of. From there we can further filter the results to just get to what we want to modify.
PS C:\> Get-NetAdapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet 1 Realtek USB GbE Family Controller #1 14 Up 98-E7-43-C1-13-5E 1 Gbps Ethernet 2 Realtek USB GbE Family Controller #2 15 Disconnected 98-E7-43-3C-7A-00 1 Gbps
In this particular server, we have two network interfaces to worry about. To make sure that we are only working on the one we want, let’s make sure to just get our
Ethernet 1 interface and save it to a variable,
$Adapter for later use.
PS C:\> $Adapter = Get-NetAdapter -Name "Ethernet 1" PS C:\> $Adapter Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet 1 Realtek USB GbE Family Controller #1 14 Up 98-E7-43-C1-13-5E 1 Gbps
Disabling Unnecessary Network Interfaces
Earlier we mentioned that there were two network interfaces on this system. Well, we don’t need to use the second one. Though it may be used in the future, it’s best to avoid confusion, so let’s disable the interface. After that, let’s verify that it really is disabled.
PS C:\> Disable-NetAdapter -Name "Ethernet 2" PS C:\> Get-NetAdapter -Name "Ethernet 2" Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet 2 Realtek USB GbE Family Controller #2 15 Disabled 98-E7-43-3C-7A-00 1 Gbps
Configuring a Static IP Address
Now that we have the network interface we want, we need to assign the IP addressing information that we were given. Our network team gave us the following IP details to assign.
- IP Address:
- Prefix Length:
Given the above details, we can assign these values to our network interface using the
New-NetIPAddress cmdlet. Using the previously stored adapter in our
$Adapter variable, we can quickly update the network interface configuration.
$Params = @( "IPAddress" = '10.0.0.100' "DefaultGateway" = '10.0.0.1' "PrefixLength" = 24 "InterfaceIndex" = $Adapter.InterfaceIndex ) New-NetIPAddress @Params
Before we move on though, we need to make sure that the configuration took correctly. Using our same
$Adapter variable we can pull back just the information we want from our interface.
PS C:\> Get-NetIPConfiguration -InterfaceIndex $Adapter.InterfaceIndex InterfaceAlias : Ethernet 1 InterfaceIndex : 14 InterfaceDescription : Realtek USB GbE Family Controller #1 NetProfile.Name : IPv4Address : 10.0.0.100 IPv6DefaultGateway : IPv4DefaultGateway : 10.0.0.1 DNSServer :
Renaming a Network Adapter
If you have multiple network adapters on a system, it can be very useful to name them according to their function. In this case, we want to rename our first adapter to that of
Internal Network. This will help distinguish it from our secondary adapter when we do bring that only eventually. Thankfully, there is a handy
Rename-NetAdapter function that we can use. It doesn’t take the
InterfaceIndex unfortunately and you must use the name of the interface.
PS C:\> Rename-NetAdapter -Name "Ethernet 1" -NewName "Internal Network" PS C:\> Get-NetAdapter -Name "Internal Network" Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Internal Network Realtek USB GbE Family Controller #1 14 Up 98-E7-43-C1-13-5E 1 Gbps
Setting Custom DNS Servers
Though we have set up our new interface, something isn’t quite right about our DNS Servers, notably that we have none! In this case, we need to set those manually, to the following that have been provided by our networking team.
- DNS Server 1:
- DNS Server 2:
Set-DNSClientServerAddress we can define our DNS Server addresses to be used on the given interface. You may notice the use of the
[IPAddress] accelerator, this will validate the IP Address and throw an error in case we mistyped or used a wrong one.
$DNSServers = @( ([IPAddress]"184.108.40.206").IPAddressToString ([IPAddress]"220.127.116.11").IPAddressToString ) Set-DNSClientServerAddress -InterfaceIndex $Adapter.InterfaceIndex -ServerAddresses $DNSServers
After we run
Get-NetIPConfiguration one more time, we can see that our DNS addresses are now set! As you can see below the
InterfaceAlias property is showing our new name that we previously set as well.
PS C:\> Get-NetIPConfiguration -InterfaceIndex $Adapter.InterfaceIndex InterfaceAlias : Internal Network InterfaceIndex : 14 InterfaceDescription : Realtek USB GbE Family Controller #1 NetProfile.Name : IPv4Address : 10.0.0.100 IPv6DefaultGateway : IPv4DefaultGateway : 10.0.0.1 DNSServer : 18.104.22.168 22.214.171.124
Managing Network Interfaces Using PowerShell 7
Though this is just the beginning, if you were to take this one step further you could script these tasks to apply to multiple servers. If you have been given the task to provision a large number of computers, by utilizing these cmdlets, you would be able to make short work of that task. This can save countless hours stuck in GUI’s and ultimately make you, the administrator, far more productive.
As you can tell, it’s trivial to manage our Network Interface within PowerShell 7. An IT administrator can make quick work of automating the configuration and setup using the many available cmdlets to do so!