Changing network settings is not something I do often so I resort to going to the control panel because I am familiar with it and faster than discovering the PowerShell commands, that’s about to change.
PowerShell offers a powerful way to manage network settings on Windows machines. Here’s a quick guide on how to change your IP address, retrieve network information similar to ipconfig
, and set your network adapter back to DHCP using PowerShell commands.
Getting network settings
These examples use a made up name of lab for the network adapter name, your setup will be different. You can also use the interfaceIndex parameter to specify the adapter index instead as you’ll see in some examples.
Finding Network Adapter Details
To find the name (InterfaceAlias
) and index (InterfaceIndex
) of your network adapters, you can use the Get-NetAdapter
cmdlet.
PS C:\> Get-NetAdapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
lab USB to Ethernet Adap 22 Up 12-34-56-78-9A-BC 100 Mbps
vEthernet (Internal) Virtual Ethernet Adap 15 Up AB-CD-EF-12-34-56 1 Gbps
LAN PCIe GbE Controlle 7 Up 98-76-54-32-10-FE 1 Gbps
This command will list all network adapters along with their names, aliases, and indexes, helping you identify the correct adapter to configure. IT will even give you the MAC address and speed of the link.
You can then select the properties you want using select-object and the InterfaceIndex parameter for get-NetAdapter command to choose a specific adapter
PS C:\> Get-NetAdapter -InterfaceIndex 22 | Select-Object Name, Ifindex, macaddress, status, speed
Name : lab
ifIndex : 22
MacAddress : 12-34-56-78-9A-BC
Status : Up
speed : 100000000
Getting Network Information
To get network information similar to ipconfig
, including DNS and subnet prefix, you can use the Get-NetIPAddress
and Get-DnsClientServerAddress
cmdlets. But
# Get IP address, subnet prefix, and other details
Get-NetIPAddress -InterfaceAlias "lab"
# Get DNS server addresses
Get-DnsClientServerAddress -InterfaceAlias "lab"
These commands will display the current IP configuration, including the IP address, subnet prefix, and DNS server addresses. If you have IPv6enabled then you will also get the IPv6 details, so to limit the results to IPv4 you can use the AddressFamily parameter:
Get-NetIPAddress -InterfaceAlias "lab" -AddressFamily ipv4
Displays current TCP/IP network configuration
A better option to view IP configuration is the following command which gives returns IP addresses, default gateways and DNS servers:
Get-NetIPConfiguration -Detailed
Changing network settings
Changing the IP Address
To change the IP address of a network adapter, you can use the New-NetIPAddress
cmdlet. This command allows you to set a static IP address, subnet mask, and default gateway.
New-NetIPAddress -InterfaceAlias Lab -IPAddress 192.168.10.115 -PrefixLength 24 -DefaultGateway 192.168.10.1
In this example:
InterfaceAlias
specifies the network adapter.IPAddress
is the new IP address.PrefixLength
is the subnet mask (24 means 255.255.255.0).DefaultGateway
is the gateway address.
You can verify results in the control panel settings for the adapter:
Changing preferred DNS server addresses
Similarly you can also change the DNS settings using the following command:
Set-DnsClientServerAddress -InterfaceAlias "lab" -ServerAddresses 8.8.8.8, 8.8.4.4
Setting Adapter Back to DHCP
If you need to set your network adapter back to obtain an IP address automatically from a DHCP server, use the Set-NetIPInterface
cmdlet.
Set-NetIPInterface -InterfaceAlias "lab" -Dhcp Enabled
This command will configure the network adapter to use DHCP for obtaining its IP address.
However this won’t reset the DNS server addresses, so you can the same command you used before to change the DNS server addresses but with the ResetServerAddresses
parameter.
Set-DnsClientServerAddress -InterfaceAlias "lab" -ResetServerAddresses
Other functions
As a bonus here are some more handy commands.
Restart adapter
To renew your IP address in PowerShell, you can use the Restart-NetAdapter
cmdlet, which effectively releases and renews the IP address for a network adapter similar to ipconfig /renew.
Restart-NetAdapter -Name "Ethernet"
Disable DHCP
To release you IP address you can use the following:
Set-NetIPInterface -InterfaceIndex 12 -Dhcp Disabled
Clears the contents of the DNS client cache
This is equivalent to running ipconfig /flushdns:
Clear-DnsClientCache
Conclusion
Using PowerShell to manage network settings can save time and allow for automation. Whether you need to change an IP address, retrieve detailed network information, or switch back to DHCP, these PowerShell commands provide a robust solution.
Ipconfig still has it’s uses and it’s a convenient and quick way to check network settings and do things like clear DNS resolver cache, renew and release your IP address. Since everyone is familiar with ipconfig it’s hard to make the change and have to learn the PowerShell alternatives. That said it’s easier to work with PowerShell’s object-oriented output than ipconfig.
There is also the Test-NetConnection
command but I’ll leave that one for another day.