This one seems confusing for most, as it might be the area where most Windows sysadmins rely on the GUI. If you ask (almost) any sysadmin how to change the IP on a server, they are going to answers how to get to the network adapters in the settings. Things like this seem to be to be one of the reasons why people are afraid of adapting Server Core.
Figuring that it’s time to inform the people and make sure that anyone can handle networking, even if they can only access the shell. Let’s summarize how to do most network related tasks in PowerShell.
Enable and Disable NIC
# List all network adapters
Get-NetAdapter
# Disable a specific network adapter, for instance the Wi-Fi adapter
# First by name, then by piping a specific adapter
Disable-NetAdapter -Name "Wi-Fi"
Get-NetAdapter -InterfaceIndex 5 | Disable-NetAdapter
# Activate a specific network adapter
# Again by name and then by piping a specific adapter
Enable-NetAdapter -Name "Wi-Fi"
Get-NetAdapter -InterfaceIndex 5 | Enable-NetAdapter
Get and set IP address
# Get the IP-address of a specific adapter
Get-NetIPAddress -InterfaceIndex 5
# Get just the IPv4-address
Get-NetIPAddress -InterfaceIndex 5 -AddressFamily IPv4
# Just the address itself
(Get-NetIPAddress -InterfaceIndex 5 -AddressFamily IPv4).IPAddress
# Set IPv4-address, using splatting for better readability
$ipParameter = @{
InterfaceIndex = 22
IPAddress = "10.0.0.22"
PrefixLength = 24
AddressFamily = "IPv4"
}
New-NetIPAddress @ipParameter
# Set the adapter to DHCP
Set-NetIPInterface -InterfaceIndex 22 -Dhcp Enabled
Set DNS server for NIC and reset DNS Cache
# Set DNS-server addresses on a specific NIC
$dnsParameter = @{
InterfaceIndex = 5
ServerAddresses = ("8.8.8.8","8.8.4.4")
}
Set-DnsClientServerAddress @dnsParameter
# Clear DNS cache
Clear-DnsClientCache