I haven't located a good cheat sheet for basic PowerShell commands to manage your Network Card/IP stack, so I thought I should make one. If you like this please share it with your mom because I'm sure she's been waiting to set a static IP address via PowerShell for some time. Also you should call her more.
- Windows 2012/Windows 8 or higher
- Administrator access to the machine in question
- Powershell ran as administrator on the machine in question
Most of the commands used to manipulate IP settings can be found by typing Get-Command -Module NetTCPIP
To find out more about any given command, type Get-Help <cmdlet name>
PS C:\WINDOWS\system32> Get-Command -Module NetTCPIP CommandType Name ModuleName ----------- ---- ---------- Function Find-NetRoute NetTCPIP Function Get-NetCompartment NetTCPIP Function Get-NetIPAddress NetTCPIP Function Get-NetIPConfiguration NetTCPIP Function Get-NetIPInterface NetTCPIP Function Get-NetIPv4Protocol NetTCPIP Function Get-NetIPv6Protocol NetTCPIP Function Get-NetNeighbor NetTCPIP Function Get-NetOffloadGlobalSetting NetTCPIP Function Get-NetPrefixPolicy NetTCPIP Function Get-NetRoute NetTCPIP Function Get-NetTCPConnection NetTCPIP Function Get-NetTCPSetting NetTCPIP Function Get-NetTransportFilter NetTCPIP Function Get-NetUDPEndpoint NetTCPIP Function Get-NetUDPSetting NetTCPIP Function New-NetIPAddress NetTCPIP Function New-NetNeighbor NetTCPIP Function New-NetRoute NetTCPIP Function New-NetTransportFilter NetTCPIP Function Remove-NetIPAddress NetTCPIP Function Remove-NetNeighbor NetTCPIP Function Remove-NetRoute NetTCPIP Function Remove-NetTransportFilter NetTCPIP Function Set-NetIPAddress NetTCPIP Function Set-NetIPInterface NetTCPIP Function Set-NetIPv4Protocol NetTCPIP Function Set-NetIPv6Protocol NetTCPIP Function Set-NetNeighbor NetTCPIP Function Set-NetOffloadGlobalSetting NetTCPIP Function Set-NetRoute NetTCPIP Function Set-NetTCPSetting NetTCPIP Function Set-NetUDPSetting NetTCPIP Function Test-NetConnection NetTCPIP
Here are walkthroughs of some of the more common tasks you may want to perform.
List Network AdaptersGets a list of all adapters in the machine; you'll need to know your adapter name or index # (both listed) for some of the commands below.
Change an Adapter Friendly NameYou may want to consider changing the friendly name of the adapter you intend to manipulate. By giving it shorter, more meaningful name you'll have an easier time going forward. Use the old name you got from the last command. Syntax is Rename-NetAdapter -Name "<Current Name>" -NewName <"New Name">
Rename-NetAdapter -Name "Local Area Connection" -NewName WiFi
Get the Current IP AddressGets all IPv4 addresses on the machine; you'll almost always have multiple as your loopback interface (127.0.0.1) will be listed. Ignore that guy.
Optionally, you can specify -InterfaceAlias <friendly name> or -InterfaceIndex <index #> to limit the command to a single adapter.
Get-NetIPAddress -AddressFamily ipv4
Assign a Static IP Address to your Network AdapterThis command will set the one and only (overwriting what is there) IP address for the specified network adapter. You also can (and should) set the subnet mask with this command. The subnet mask is set via CIDR using the -PrefixLength; see the link for more info about CIDR, but if you're not familiar with CIDR it is likely that you want -PrefixLength 24 which translates to 255.255.255.0 meaning the first three octets are the network while the last is the host. Syntax is New-NetIPAddress -InterfaceAlias <name> -IPAddress <IP address> -PrefixLength <CIDR> -DefaultGateway <Gateway IP> . You can substitute -InterfaceIndex <index #> for -InterfaceAlias .
Note: You will get an error if you already have a static IP address with a default gateway. To fix this problem see "Delete Existing Static IP" below and then try again.
New-NetIPAddress -InterfaceAlias WiFi -IPAddress 192.168.1.10 -PrefixLength 24 -DefaultGateway 192.168.1.1
Note2: We're not using "Set-NetIPAddress" here because it doesn't allow you to set a default gateway. BOOOO.
Set DNS Servers for your AdapterTo look up names you'll need to set DNS server(s). Syntax is Set-DNSClientServerAddress -InterfaceAlias <name> -ServerAddresses ("<IP Address 1","IP Address 2") . You can set as many DNS servers as you like. You can substitute -InterfaceIndex <index #> for -InterfaceAlias .
Set-DNSClientServerAddress –interfaceAlias WiFi –ServerAddresses (“192.168.1.5”,”192.168.1.6”)
Set a Default GatewayIt's generally easier to set the default gateway as part of the New-NetIPAddress command above, but if you want to set one separately see "Set a Static Route" below.
Delete Existing Static IP (to prep for a new)This is a two step process; you need to delete the IP, then the gateway. No need to worry about the DNS servers here as it works to overwrite them with the command above. You will need to know the IP address you want to delete first; use get-netipaddress (above) to get it (write it down to use below if necessary). You'll then need to know the NextHop of the gateway. To get this, use the get-netroute command and write down the entry(ies) that have a nexthop of the gateway you intend to remove (see screenshot).
The syntax for these commands are
Remove-NetAddress <IPAddress> -Confirm:$False
Remove-NetRoute -NextHop <Gateway IPAddress> -Confirm:$False
Note: If you have multiple routes set with that default gateway it will delete them all. If you haven't manually set routes, don't worry about it (you just have the one).
Remove-NetAddress 192.168.1.10 -Confirm:$False Remove-NetRoute -NextHop 192.168.1.1 -Confirm:$False
Set Your Adapter to Use DHCPThis is another two step process; first set the desired adapter IP/Gateway to DHCP then set the DNS servers to pull from DHCP as well.
The syntax for these commands are:
Set-NetIPInterface -InterfaceAlias <name> -Dhcp Enabled
Set-DNSClientServerAddress -InterfaceAlias <name> -ResetServerAddress
You can substitute -InterfaceIndex <index #> for -IterfaceAlias if you prefer.
Note: If you have a static gateway set you'll need to perform the second step "Remove-NetRoute" from the step above as well.
Set-NetIPInterface -InterfaceAlias WiFi -Dhcp Enabled Set-DNSClientServerAddress -InterfaceAlias Wifi -ResetServerAddress
Here are walkthroughs of some of the more common tasks you may want to perform. My assumption here is that you know what you want to do so I won't be discussing the details of what each of these means.
Add/Delete a Static RouteAdd: (use -RouteMetric to specify metric or -PolicyStore to control persistence through reboots)
Add default route:
New-NetRoute -DestinationPrefix 192.168.2.0/24 -InterfaceAlias WiFi -NextHop 192.168.2.1
Delete: (while this command is very specific you can be more generic; see above)
New-NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceAlias WiFi -NextHop 192.168.2.1
Remove-NetRoute -DestinationPrefix 192.168.2.0/24 -InterfaceAlias WiFi -NextHop 192.168.2.1
Test Network Connectivity (Ping)Test-Connection replaces ping.exe. In addition to the ping functionality Test-Connection supports authentication (if firewall is set accordingly), multiple targets in a single command, running as a job, as well as more detailed returns.
Assign a DNS SuffixThis is the domain under which your IP will be registered and under most circumstances will be used to append to hostname searches. Note this is per-adapter. You can substitute InterfaceIndex for InterfaceAlias if you like.
Set-DnsClient -InterfaceAlias WiFi -ConnectionSpecificSuffix mydomain.com
Assign an Additional IP Address to your NICIf you want to add another IP (usually only applicable on a server)
New-NetIPAddress -InterfaceAlias Ethernet1 -IPAddress 192.168.1.101 -PrefixLength 24
References/More InformationTechNet: Net TCP/IP Cmdlets in Windows PowerShell
TechNet: DNS Client Cmdlets in Windows PowerShell
TechNet: Network Adapter Cmdlets
You did it, hug a puppy!