Admin Power Level: Intermediate Chapter 15: Small Commands, Big Everyday Wins
Network: adapters, IP addresses, and DNS
Network settings live on adapters. PowerShell reads adapters, reads and sets IP addresses, reads and sets DNS servers, and changes the default route, all per adapter.
Lesson 3 of 5 in Chapter 15: Small Commands, Big Everyday Wins. Previous step in this path: Fine grained password policies (FGPP). 3 learning objectives, 6 examples, and a knowledge check.
Start: what you will learn
Network settings live on adapters. PowerShell reads adapters, reads and sets IP addresses, reads and sets DNS servers, and changes the default route, all per adapter.
Learning objectives
- List adapters and read their IP settings
- Set a static IP address on an adapter
- Read and set DNS server addresses
Concept in simple words
What it is: network settings live on adapters, which are the network connections of a computer, such as Ethernet or Wi-Fi. An adapter is the doorway through which that computer talks to a network. PowerShell can list adapters, read and set IP addresses, read and set DNS server addresses, and change the default route, which is the path traffic takes when no more specific route matches. Every one of these jobs is per adapter, so the first question is always: which adapter do you mean.
What problem it solves: in a graphical settings window, you click through one computer at a time, and it is easy to miss a detail or to set the same value differently on two computers. With PowerShell, you can list, read, and set the same settings in a repeatable way, and you can read them back to prove what changed. That matters most for servers and lab computers, where a fixed, known address is part of the setup, not a nice extra.
The four pieces of a normal IPv4 setup, in plain words: an IP address identifies a computer on a network, like a house number. A prefix length says how big the local network is, like how long the street is. A default gateway is the router that leads out of that local network, like the main road out of your neighbourhood. DNS, which stands for Domain Name System, is separate from all of that, and it turns names into addresses, like a phone book that turns a person's name into a number you can call. You need all four pieces to agree, or the computer will only partly work.
Prefix length, explained slowly: a prefix length of 24 means the first 24 bits of the address name the network, and the rest name the computer inside that network. In the familiar dotted form, a prefix length of 24 matches a subnet mask of 255.255.255.0, and it gives a small network with room for up to 254 usable computer addresses, plus the network address itself and the broadcast address. A prefix length of 16 would be a much bigger network, and a prefix length of 25 would be a smaller one. That is why a typo in this one number can put a computer on the wrong network size, even when the IP address itself looks right.
DHCP versus static, the choice you make first: DHCP means automatic, where a server hands the computer an address, a gateway, and DNS servers, and the address may change over time. Static means fixed, where you type the address yourself and it stays put. Laptops and ordinary office computers usually suit DHCP, because they move around. Servers, printers, and lab computers often suit static, because other computers need to find them at the same address every time. Before you set a static address, check that no other computer already uses it, because two computers with the same address will fight over it and both will behave strangely.
What PowerShell does behind the command: Get-NetAdapter asks Windows for the list of adapters, with name, status, and speed. Get-NetIPAddress asks for the IP configuration of one adapter. New-NetIPAddress creates a new address on an adapter, while Set-NetIPAddress changes an existing address, and that difference matters, as you will see below. Get-DnsClientServerAddress and Set-DnsClientServerAddress handle DNS separately from the IP address, and Get-NetRoute reads the routing table, including the default route, which has the destination prefix 0.0.0.0/0, meaning every address that has no better route.
Think of it like this
Think of it like this: a street address and a phone book. The IP address is your house number on your street, the prefix length says how long your street is, the default gateway is the main road out of your neighbourhood, and DNS is the phone book that turns a person's name into a number you can call. Changing your house number does not rewrite the phone book, and changing the phone book does not move your house, which is why IP and DNS are two separate jobs, even though you often do them in the same session.
How it works, step by step
How it works, step by step, part one: First, list adapters with Get-NetAdapter and pick the exact adapter name, quoting it if it has spaces. Note that name down, and also note the current settings before you change anything, because your old settings are your way back if the new ones do not work. Second, read the current IP settings with Get-NetIPAddress for that adapter, and read DNS with Get-DnsClientServerAddress, so you know what you are changing from. Third, if you need a static address, set it with New-NetIPAddress for a new address, or Set-NetIPAddress to change one that already exists, giving the address, the prefix length, and usually the default gateway.
How it works, step by step, part two: Fourth, read and set DNS separately with Get-DnsClientServerAddress and Set-DnsClientServerAddress, and check the result after each change. Fifth, read the routing table with Get-NetRoute, and check that the default route points at the gateway you intended. Sixth, test in stages: first the gateway, then a name on the internet, so you can tell whether a failure is local, a gateway problem, or a DNS problem. If you only test a name and it fails, you will not know which of the three pieces broke.
Let us walk through the first example: take Get-NetAdapter | Select-Object Name, Status, LinkSpeed. Get-NetAdapter lists every adapter, and the pipe sends that list to Select-Object, which keeps only Name, Status, and LinkSpeed. Status tells you whether the adapter is Up or Down, and LinkSpeed tells you how fast the link is, such as 1 Gbps. In the output, look for the exact name of the adapter you want, including spaces and capitals, because later commands must match that name. If you change the selection to include more properties, you will see more detail, but Name is the value the later commands need most.
Let us walk through the second example: take New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress "192.168.1.50" -PrefixLength 24 -DefaultGateway "192.168.1.1". -InterfaceAlias names the adapter, and quoting matters if a name has spaces. -IPAddress gives the new static address, -PrefixLength 24 describes a typical small network, and -DefaultGateway gives the router out of that network. In the output, look for a confirmation naming the address and the adapter, then read the settings back, because a typo in one number can put the computer on the wrong network. If you change PrefixLength from 24 to 16, you have not just changed a number, you have told the computer its local network is far bigger, which changes which addresses it treats as local.
Let us walk through the third example: take Get-DnsClientServerAddress -InterfaceAlias "Ethernet" | Select-Object InterfaceAlias, ServerAddresses. Get-DnsClientServerAddress reads the DNS servers for an adapter. -InterfaceAlias names the adapter, using the same adapter name pattern as the lesson examples. Select-Object keeps only the adapter name and the ServerAddresses list, so you can see at a glance which DNS servers that adapter is using. If this list is empty after a change, DNS lookups will fail even though the IP address itself is fine, which is a classic clue that IP and DNS are separate jobs.
Let us walk through the fourth example: take Get-NetIPConfiguration -InterfaceAlias "Ethernet". This is the combined view, and it is the fastest way to check a whole adapter at once, because it brings the adapter name, the IPv4 address, the default gateway, and the DNS servers together in one result. Use it as your after check: run it before a change to record the old values, and run it again after a change to prove the new values. If one value in this combined view looks wrong, go back to the single purpose command for that value, such as Get-NetIPAddress for the address, or Get-DnsClientServerAddress for DNS, and fix that one piece.
Let us walk through the fifth example: take Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "192.168.1.1", "192.168.1.2". -InterfaceAlias names the adapter, and -ServerAddresses takes a list, with the first server listed tried first and the second used as a fallback. The comma between the two addresses is what makes them a list, not one long address, so do not forget it. After this command, read DNS back with Get-DnsClientServerAddress, because a typo in a DNS address gives a confusing symptom: the computer can reach numbers directly, but names stop working.
Let us walk through the sixth example: take Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Select-Object DestinationPrefix, NextHop, InterfaceAlias. -DestinationPrefix "0.0.0.0/0" filters the routing table to the default route only, which is the route for every address with no better match. NextHop is the gateway that traffic takes, and InterfaceAlias tells you which adapter sends it. In the output, check that NextHop equals the gateway you set, and that InterfaceAlias equals the adapter you changed. If NextHop points at a different gateway, your traffic is leaving through a door you did not intend.
New versus Set, a distinction that catches many learners: New-NetIPAddress creates and adds an address, while Set-NetIPAddress changes an existing address. If an address already exists and you run New-NetIPAddress again with a different address, you may end up with two addresses on the same adapter, with the old one still in place, instead of a clean replacement. When you mean to replace, check the current list first, change the existing address with Set-NetIPAddress, or remove the old address first with Remove-NetIPAddress, and then read the list back. Never guess which case you are in, because the list after the command tells you the truth.
Getting back to automatic: if a static setup was only a test, you can return an adapter to DHCP with Set-NetIPInterface -InterfaceAlias "Ethernet" -Dhcp Enabled, and you can return DNS to automatic with Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses. Both are useful undo steps, but they are still changes, so note the static values down before you undo them, in case you need to put them back. A clean undo is part of good admin work, not a sign of failure.
Safety warning, read this before you act: only change network settings on your own computer, with permission, and only after you have written down the old settings. A wrong address, a wrong prefix length, or a wrong gateway can disconnect the computer, and if you are changing settings over the same remote connection you are changing, a wrong address can disconnect you in the middle of the job. Prefer a console session for network changes, which means sitting at the computer itself, or a session that does not depend on the network you are changing. If you must work remotely, have a second way back in, and a person nearby who can help if the connection drops.
Edge cases to watch for: adapter names with spaces must be quoted, or PowerShell will read the name as two separate pieces. An adapter index number can differ from one computer to another, so prefer the name in any command you plan to reuse, and check the name on each computer first. A Wi-Fi adapter and an Ethernet adapter can both be Up at once, so setting an address on the wrong one is easy if you guess. And a computer with two gateways can send traffic through the wrong one, so always check the default route after a change, not just the IP address.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-NetIPAddress -InterfaceAlias <Adapter>Example 1
Lists adapters with status and speed, so you can pick the right adapter name for later commands. Name is the value later commands need most, Status shows Up or Down, and LinkSpeed shows the link speed, such as 1 Gbps. Copy the exact name, including spaces, for the next commands.
Get-NetAdapter | Select-Object Name, Status, LinkSpeedIllustrative output: Name Status LinkSpeed ---- ------ --------- Ethernet Up 1 Gbps
Example 2
Sets a static IPv4 address on Ethernet, with a 24 bit prefix and a default gateway. -InterfaceAlias names the adapter, -IPAddress gives the new address, -PrefixLength 24 describes a typical small network, and -DefaultGateway gives the router out of that network. Read the settings back after, because one typo can put the computer on the wrong network.
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress "192.168.1.50" -PrefixLength 24 -DefaultGateway "192.168.1.1"Illustrative output: Address 192.168.1.50 set on Ethernet.
Example 3
Get-DnsClientServerAddress reads the DNS servers for an adapter. -InterfaceAlias names the adapter, using the same adapter name pattern as the lesson examples. Select-Object keeps only the adapter name and the ServerAddresses list, so you can see at a glance which DNS servers that adapter is using.
Get-DnsClientServerAddress -InterfaceAlias "Ethernet" | Select-Object InterfaceAlias, ServerAddressesIllustrative output: InterfaceAlias ServerAddresses -------------- --------------- Ethernet 192.168.1.1
Example 4
Shows the combined view for one adapter: its name, IPv4 address, default gateway, and DNS servers together. Use it before a change to record the old values, and again after a change to prove the new values. If one value looks wrong, fix that one piece with its single purpose command.
Get-NetIPConfiguration -InterfaceAlias "Ethernet"Illustrative output: InterfaceAlias: Ethernet IPv4Address: 192.168.1.50 DefaultGateway: 192.168.1.1 DNSServer: 192.168.1.1
Example 5
-InterfaceAlias names the adapter, and -ServerAddresses takes a list, with the first server tried first and the second as a fallback. The comma between the addresses is what makes them a list, so do not forget it. Only change DNS on your own computer, with permission, and read DNS back after, because a typo here breaks names while numbers still work.
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses "192.168.1.1", "192.168.1.2"Illustrative output: DNS servers set on Ethernet.
Example 6
-DestinationPrefix "0.0.0.0/0" filters to the default route only, which is the route for every address with no better match. NextHop is the gateway traffic takes, and InterfaceAlias shows which adapter sends it. Check that NextHop equals the gateway you set, and that the adapter is the one you changed.
Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Select-Object DestinationPrefix, NextHop, InterfaceAliasIllustrative output: DestinationPrefix NextHop InterfaceAlias ----------------- ------- -------------- 0.0.0.0/0 192.168.1.1 Ethernet
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Setting an IP address on the wrong adapter, because you guessed the name instead of listing adapters first.
- Changing IP settings remotely over the same connection you are changing, which can disconnect you. Prefer a console session for network changes.
- Setting a static IP address but forgetting the default gateway, so the computer can talk to its local network but cannot reach the internet or any other network.
- Setting an IP address but forgetting DNS, so numbers work but names fail, and you blame the IP address for a DNS problem. IP and DNS are separate jobs, so check both.
- Running New-NetIPAddress to replace an existing address, and ending up with two addresses on the adapter, because New adds while Set changes. Check the list first, and read it back after.
- Using an address another computer already uses, so both computers behave strangely. Check that a static address is free before you assign it, and note the old settings down first.
- Using a wrong prefix length, such as 16 when you meant 24, so the computer treats a far bigger range as local, and traffic that should go through the gateway never reaches it. Read the prefix length back after every change.
Knowledge check
Knowledge check
Which cmdlet shows the DNS servers for an adapter?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: adapter first, read before you write, IP and DNS are separate jobs, New adds while Set changes, and always read the settings back and test in stages. That order keeps a small network change from becoming a lost connection, and it gives you a clear clue, local, gateway, or DNS, whenever something does not work after a change.
Next step: Host, files, time, and keyboard