PowerShell Get IP Address: Commands to Find, Filter and Set IP Addresses
Short answer
Run Get-NetIPAddress -AddressFamily IPv4 in PowerShell to list the IPv4 address of every network interface; the Wi-Fi or Ethernet row is your computer’s local IP. (Get-NetIPConfiguration | Where-Object IPv4DefaultGateway).IPv4Address.IPAddress prints only the address of the connected adapter, and Get-NetIPConfiguration alone also shows the default gateway and DNS servers. Your public IPv4 address belongs to your router, so it takes an outside lookup, such as an API call with Invoke-RestMethod.
On this page
- Get your IP address in PowerShell with Get-NetIPAddress
- PowerShell one-liners for IP address, gateway and DNS
- Show the gateway, DNS servers and full configuration with Get-NetIPConfiguration
- Get your public IP address in PowerShell
- Get the IP address from a hostname, or the hostname from an IP
- Set a static IP address with PowerShell
- Get the IP address of a remote computer
- Test whether an IP address is reachable
- Get the IP address in PowerShell 7 on macOS and Linux
- Troubleshooting PowerShell IP commands
PowerShell returns IP settings as objects, so you can filter them, keep them in a variable and reuse them in scripts. This page covers local and public IPs, gateway and DNS servers, DNS lookups, static IP and DHCP, remote computers and reachability tests, plus fixes for common errors. The networking cmdlets come with Windows 10, Windows 11 and Windows Server and work in Windows PowerShell 5.1 and PowerShell 7. ipconfig runs in PowerShell too, but it returns plain text; for Settings, ipconfig and the other built-in tools, see how to find your IP address on Windows.
Get your IP address in PowerShell with Get-NetIPAddress
Right-click Start and select Terminal (on Windows 10, Windows PowerShell). Reading IP settings doesn’t need administrator rights. Run:
Get-NetIPAddress -AddressFamily IPv4 | Format-Table InterfaceAlias, IPAddress, PrefixOriginThe output looks like this (example values):
InterfaceAlias IPAddress PrefixOrigin
-------------- --------- ------------
vEthernet (Default Switch) 172.20.144.1 Manual
Wi-Fi 192.168.1.23 Dhcp
Ethernet 169.254.83.107 WellKnown
Loopback Pseudo-Interface 1 127.0.0.1 WellKnownHow to read it:
- Wi-Fi or Ethernet with Dhcp is your computer’s local IP, assigned by your router. Manual means someone typed the address in.
- 127.0.0.1 is the loopback address every PC has.
- 169.254.x.x means the adapter got no answer from a DHCP server. On a disconnected adapter, that’s normal.
- vEthernet adapters belong to Hyper-V or WSL. They connect virtual machines, not your PC, to a network.
These are local (private) addresses that work only inside your network; local vs. public IP addresses explains the difference. Add PrefixLength to the column list to see the subnet size (24 means 255.255.255.0). With -AddressFamily IPv6 you get the IPv6 addresses; those starting with fe80 are link-local. Microsoft’s Get-NetIPAddress reference lists every parameter.
PowerShell one-liners for IP address, gateway and DNS
| Task | Command |
|---|---|
| All IPv4 addresses | Get-NetIPAddress -AddressFamily IPv4 |
| IPv4 address of the connected adapter | (Get-NetIPConfiguration | Where-Object IPv4DefaultGateway).IPv4Address.IPAddress |
| IPv4 address of one adapter | (Get-NetIPAddress -InterfaceAlias 'Wi-Fi' -AddressFamily IPv4).IPAddress |
| IPv6 addresses | Get-NetIPAddress -AddressFamily IPv6 |
| Default gateway (router) | (Get-NetRoute -DestinationPrefix '0.0.0.0/0').NextHop |
| DNS servers | Get-DnsClientServerAddress -AddressFamily IPv4 |
| MAC address and link status | Get-NetAdapter |
| Address Windows uses for the internet | (Find-NetRoute -RemoteIPAddress 1.1.1.1)[0].IPAddress |
| Devices your PC has talked to (ARP cache) | Get-NetNeighbor -AddressFamily IPv4 -State Reachable, Stale |
Find-NetRoute only consults the routing table: it returns the local address and the route Windows would use to reach 1.1.1.1, without sending anything there. Get-NetNeighbor is the PowerShell counterpart of arp -a. It lists the IP and MAC address (LinkLayerAddress) of devices on your network that your PC contacted recently.
Filter out loopback, 169.254 and virtual adapters
Get-NetIPAddress -AddressFamily IPv4 |
Where-Object {
$_.IPAddress -ne '127.0.0.1' -and
$_.IPAddress -notlike '169.254.*' -and
$_.InterfaceAlias -notlike 'vEthernet*'
} |
Select-Object InterfaceAlias, IPAddress, PrefixLength, PrefixOriginAdd a line such as $_.InterfaceAlias -notlike '*VPN*' to skip VPN adapters. A shorter filter is Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp, Manual. It keeps only addresses from a DHCP server or typed in by hand, which drops 127.0.0.1 and 169.254 addresses, since Windows marks both as WellKnown. vEthernet addresses count as Manual, so they stay.
Return the IP address as a plain string
Cmdlets return objects. Select-Object IPAddress keeps an object with one property, which prints with a header. -ExpandProperty or the dot notation returns the bare string that a script needs:
# Object with an IPAddress property (prints a header)
Get-NetIPAddress -InterfaceAlias 'Wi-Fi' -AddressFamily IPv4 | Select-Object IPAddress
# Plain string, ready for a variable
$ip = Get-NetIPAddress -InterfaceAlias 'Wi-Fi' -AddressFamily IPv4 |
Select-Object -ExpandProperty IPAddress
$ip = (Get-NetIPAddress -InterfaceAlias 'Wi-Fi' -AddressFamily IPv4).IPAddress # same result
# One address, even with several adapters connected
$ip = (Find-NetRoute -RemoteIPAddress 1.1.1.1)[0].IPAddress
"Local IP: $ip"An adapter can have more than one IPv4 address, so the first two forms can return an array. Format-Table and Format-List turn objects into display text. Put them only at the end of a command whose output you want to read, never before Where-Object or in a value you assign to a variable.
Show the gateway, DNS servers and full configuration with Get-NetIPConfiguration
Get-NetIPConfigurationIt lists each connected, non-virtual adapter with IPv4Address, IPv4DefaultGateway (your router) and DNSServer. -All adds virtual, loopback and disconnected adapters, and -Detailed adds the computer name, MAC address, network profile, MTU and DHCP status. For one adapter, run Get-NetIPConfiguration -InterfaceAlias 'Ethernet'.
For the gateway alone, Microsoft documents this command for the next hop of the default route:
Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Select-Object -ExpandProperty NextHopIt prints your router’s address, such as 192.168.1.1. Two lines mean two default routes, for example from a VPN or a second adapter, and Windows prefers the one with the lowest sum of route metric and interface metric.
Get your public IP address in PowerShell
Behind a router, your PC has only a local IPv4 address. The public one belongs to the router and is shared by every device on your network, so no local cmdlet shows it, and a server outside your network has to report it.
Without an ip parameter, the /v1/info endpoint of our IP lookup API answers for the address your request comes from. Put a free API key in an environment variable and send it in the apikey header:
$env:IPLOOKUPAPI_KEY = 'your-api-key' # current session only
$headers = @{ apikey = $env:IPLOOKUPAPI_KEY }
$r = Invoke-RestMethod -Uri 'https://api.iplookupapi.com/v1/info?fields=ip' -Headers $headers -TimeoutSec 10
$r.data.ipInvoke-RestMethod turns the JSON response into objects, so $r.data.ip is your public IP as a string. On a connection with IPv6, the request can go over IPv6, and you get your public IPv6 address instead. Add ip to look up any other address, and use fields to pick what comes back:
$uri = 'https://api.iplookupapi.com/v1/info?ip=80.110.0.1&fields=ip,location.city.name,location.country.alpha2,connection.isp,timezone.id'
$d = (Invoke-RestMethod -Uri $uri -Headers $headers -TimeoutSec 10).data
[pscustomobject]@{
IP = $d.ip
City = $d.location.city.name
Country = $d.location.country.alpha2
ISP = $d.connection.isp
TimeZone = $d.timezone.id
}IP : 80.110.0.1
City : Vienna
Country : AT
ISP : Magenta Telekom
TimeZone : Europe/ViennaThe info endpoint documentation lists every field. The free plan includes 150 requests per month and 10 per minute. A 401 error means the key is missing or wrong, and 429 means you reached a limit. IP location is an estimate: the country is usually right, the city often isn’t.
Tip
Don’t write the key into scripts you share: read it from $env:IPLOOKUPAPI_KEY and set that per session or from a secret store. Send it as a header rather than as an apikey= query parameter, because URLs end up in logs.
Without an API key, ask a DNS resolver instead. OpenDNS answers the special name myip.opendns.com with the IPv4 address the query came from:
(Resolve-DnsName -Name myip.opendns.com -Server 208.67.222.222 -Type A).IPAddress208.67.222.222 is resolver1.opendns.com. Give the server as this IPv4 address: over IPv6, OpenDNS returns no IPv4 address for the name. The method works only through the OpenDNS resolvers, and it times out on networks that block outside DNS servers. With a VPN on, both methods return the VPN server’s address.
Get the IP address from a hostname, or the hostname from an IP
Resolve-DnsName is the PowerShell counterpart of nslookup: it queries DNS and returns objects.
Resolve-DnsName -Name www.example.com -Type A # IPv4 addresses (A records)
Resolve-DnsName -Name www.example.com -Type AAAA # IPv6 addresses
Resolve-DnsName -Name www.example.com -Server 1.1.1.1 # ask a specific DNS server
(Resolve-DnsName -Name www.example.com -Type A | Where-Object Type -eq 'A').IPAddressThe Where-Object filter in the last line drops CNAME (alias) records, which have no address. To get the hostname of an IP address, query its PTR record:
Resolve-DnsName -Name 8.8.8.8 -Type PTR
(Resolve-DnsName -Name 8.8.8.8 -Type PTR).NameHost # dns.googleA name comes back only if the address has a PTR record. Many local devices and IPv6 addresses don’t have one; on a home network, it works only if your router’s DNS knows the device names. Microsoft’s Resolve-DnsName reference lists all record types and options.
The .NET methods do the same and also run in PowerShell 7 on macOS and Linux, where Resolve-DnsName doesn’t exist. They use the operating system’s resolver, including the hosts file:
[System.Net.Dns]::GetHostAddresses('www.example.com') # name to addresses
[System.Net.Dns]::GetHostEntry('8.8.8.8').HostName # address to nameSet a static IP address with PowerShell
For most home devices, a DHCP reservation on the router works better: the device keeps getting the same address, and the router never hands it to another device. Static vs. dynamic IP addresses compares both options. To set the address on the PC itself:
- Right-click Start and select Terminal (Admin) (on Windows 10, Windows PowerShell (Admin)).
- Select Yes in the User Account Control prompt.
- Run
Get-NetAdapterand note the Name of your adapter, such as Ethernet or Wi-Fi. - Run
Get-NetIPConfiguration -InterfaceAlias 'Ethernet'and note the current address, gateway and DNS servers, so you can go back. - Adjust the values in the script below to your network, then run it.
$nic = 'Ethernet' # Name from Get-NetAdapter
# Remove the adapter's current IPv4 address and default route
Remove-NetIPAddress -InterfaceAlias $nic -AddressFamily IPv4 -Confirm:$false -ErrorAction SilentlyContinue
Remove-NetRoute -InterfaceAlias $nic -DestinationPrefix '0.0.0.0/0' -Confirm:$false -ErrorAction SilentlyContinue
# New address, prefix length (24 = 255.255.255.0) and default gateway
New-NetIPAddress -InterfaceAlias $nic -IPAddress 192.168.1.50 -PrefixLength 24 -DefaultGateway 192.168.1.1
# DNS servers; separate several with commas
Set-DnsClientServerAddress -InterfaceAlias $nic -ServerAddresses 192.168.1.1New-NetIPAddress turns off DHCP for the adapter and saves the address, so it survives a restart. The gateway must be in the same subnet as the address, and the address should be outside your router’s DHCP range. -AddressFamily IPv4 leaves the IPv6 settings alone, and adding -WhatIf to the two Remove- lines shows what they would delete. Check the result with Get-NetIPConfiguration -InterfaceAlias $nic. The New-NetIPAddress reference covers the other parameters.
Warning
Over Remote Desktop or PowerShell remoting, the connection drops as soon as Remove-NetIPAddress runs, and a typo can leave the PC unreachable. Run the script at the console, or make sure you can reach the new address before you start.
Switch back to DHCP
$nic = 'Ethernet'
Remove-NetRoute -InterfaceAlias $nic -DestinationPrefix '0.0.0.0/0' -Confirm:$false
Set-NetIPInterface -InterfaceAlias $nic -AddressFamily IPv4 -Dhcp Enabled
Set-DnsClientServerAddress -InterfaceAlias $nic -ResetServerAddressesRemove the static default route first; otherwise it stays next to the one DHCP delivers. -ResetServerAddresses hands DNS back to DHCP. If Get-NetIPAddress -InterfaceAlias $nic -AddressFamily IPv4 still lists the old address as Manual, remove it with Remove-NetIPAddress -InterfaceAlias $nic -AddressFamily IPv4 -PrefixOrigin Manual -Confirm:$false.
Get the IP address of a remote computer
To see the address DNS has on record, you don’t need remoting: run Resolve-DnsName -Name PC01 -Type A. To read the computer’s own settings, run the cmdlet on it:
Invoke-Command -ComputerName PC01, PC02 -ScriptBlock {
Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp, Manual
} | Select-Object PSComputerName, InterfaceAlias, IPAddress
# Same data through a CIM session, without a script block
Get-NetIPAddress -AddressFamily IPv4 -CimSession PC01
# WMI class that also lists the gateway and DNS servers
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -ComputerName PC01 |
Select-Object PSComputerName, Description, IPAddress, DefaultIPGateway, DNSServerSearchOrderPSComputerName shows which computer each row comes from. For a list of computers, replace PC01, PC02 with (Get-Content .\computers.txt), and add | Export-Csv .\ip-addresses.csv -NoTypeInformation to save the result. In the WMI class, IPAddress holds IPv4 and IPv6 addresses in one array. Older scripts use Get-WmiObject for the same class; it doesn’t exist in PowerShell 7, and Get-CimInstance replaces it.
All three commands connect through Windows Remote Management (WinRM). In short:
- Remoting on the target: Windows Server 2012 and later accept it by default. On Windows 10 or 11, run
Enable-PSRemotingin an elevated session on the target. - Permissions: your account must be in the target’s Administrators or Remote Management Users group.
- Names and credentials: in a domain, use the computer name. With an IP address or in a workgroup, pass credentials (
-CredentialonInvoke-Command, or a session fromNew-CimSession -Credential) and add the target to WinRM’s TrustedHosts list, or use HTTPS.
Test whether an IP address is reachable
Test-Connection -ComputerName 192.168.1.1 -Count 2 # ping
Test-Connection -ComputerName 192.168.1.1 -Count 1 -Quiet # True or False
Test-NetConnection -ComputerName 192.168.1.20 -Port 3389 # TCP port, here Remote DesktopTest-NetConnection -Port reports TcpTestSucceeded. That tells you more than ping, because many firewalls drop ping but let traffic to an open port through. Test-Connection changed in PowerShell 7:
| Windows PowerShell 5.1 | PowerShell 7 | |
|---|---|---|
| Target parameter | -ComputerName | -TargetName (-ComputerName still works) |
| Output columns | Source, Destination, IPV4Address, IPV6Address, Bytes, Time(ms) | Ping, Source, Address, Latency (ms), BufferSize (B), Status |
| Force IPv4 or IPv6 | Not available | -IPv4, -IPv6 |
| TCP port test | Use Test-NetConnection -Port | -TcpPort |
| Traceroute | Use Test-NetConnection -TraceRoute | -Traceroute |
Scripts that read IPV4Address or ResponseTime from 5.1 results need changes for PowerShell 7. For ping on every operating system, see how to ping an IP address.
Get the IP address in PowerShell 7 on macOS and Linux
PowerShell 7 runs on macOS and Linux, but the NetTCPIP, DnsClient and NetAdapter modules exist only on Windows, so Get-NetIPAddress, Resolve-DnsName and Test-NetConnection aren’t available there. Test-Connection, Invoke-RestMethod and the .NET classes work everywhere. This lists the IPv4 addresses of all interfaces except loopback:
foreach ($adapter in [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()) {
foreach ($addr in $adapter.GetIPProperties().UnicastAddresses) {
if ($addr.Address.AddressFamily -eq 'InterNetwork' -and -not [System.Net.IPAddress]::IsLoopback($addr.Address)) {
[pscustomobject]@{ Interface = $adapter.Name; IPAddress = $addr.Address.ToString() }
}
}
}Use 'InterNetworkV6' for IPv6 addresses. The code works on Windows too. Native tools also run in PowerShell, such as ip -br addr on Linux and ipconfig getifaddr en0 on a Mac; the guides for finding your IP address on a Mac and on Linux explain their output.
Troubleshooting PowerShell IP commands
| Problem | Cause | Fix |
|---|---|---|
| “The term ‘Get-NetIPAddress’ is not recognized” | PowerShell on macOS or Linux, or Windows 7 and older, which lack the NetTCPIP module | On macOS and Linux, use the .NET code above; on old Windows versions, use ipconfig. The module comes with Windows 8, Windows Server 2012 and later |
| “Access is denied” when you set an address | The session isn’t elevated | Open Terminal (Admin) and run the command again |
| “Instance DefaultGateway already exists” | The adapter still has a default route | Run the Remove-NetRoute line from the static IP script, then New-NetIPAddress again |
| Two default gateways after switching to static | The old DHCP or static route stayed | List them with Get-NetRoute -DestinationPrefix '0.0.0.0/0' and remove the old one with Remove-NetRoute -DestinationPrefix '0.0.0.0/0' -NextHop <old gateway> |
| The address starts with 169.254 | No DHCP server answered | See how to fix a 169.254 IP address |
Resolve-DnsName myip.opendns.com times out | The network blocks outside DNS servers | Use the Invoke-RestMethod method instead |
| “No matching MSFT_NetRoute objects found” | There was no route to remove | Nothing to fix; -ErrorAction SilentlyContinue hides the message |
Frequently asked questions
How do I get my IP address in PowerShell?
Open PowerShell or Windows Terminal and run Get-NetIPAddress -AddressFamily IPv4. The row for your Wi-Fi or Ethernet adapter shows your computer’s local IP address, and PrefixOrigin Dhcp means your router assigned it. Get-NetIPConfiguration shows the same address together with the default gateway and DNS servers. Neither cmdlet shows your public IP, which belongs to your router.
How do I get only the IPv4 address in PowerShell?
Run (Get-NetIPConfiguration | Where-Object IPv4DefaultGateway).IPv4Address.IPAddress to print just the IPv4 address of the adapter with a default gateway, normally the one you’re connected through. For one specific adapter, use (Get-NetIPAddress -InterfaceAlias ‘Wi-Fi’ -AddressFamily IPv4).IPAddress. Both return plain strings that you can store in a variable.
How do I set a static IP address with PowerShell?
Open Terminal as administrator and find the adapter name with Get-NetAdapter. Remove the adapter’s current IPv4 address and default route with Remove-NetIPAddress and Remove-NetRoute, then run New-NetIPAddress -InterfaceAlias ‘Ethernet’ -IPAddress 192.168.1.50 -PrefixLength 24 -DefaultGateway 192.168.1.1, which also turns off DHCP for the adapter. Set the DNS servers with Set-DnsClientServerAddress -InterfaceAlias ‘Ethernet’ -ServerAddresses 192.168.1.1.
How do I get the public IP address in PowerShell?
Your public IP belongs to your router, so local cmdlets can’t show it, and a server outside your network has to report it. With an iplookupapi key in $env:IPLOOKUPAPI_KEY, run (Invoke-RestMethod -Uri ‘https://api.iplookupapi.com/v1/info?fields=ip' -Headers @{ apikey = $env:IPLOOKUPAPI_KEY }).data.ip. Without a key, (Resolve-DnsName -Name myip.opendns.com -Server 208.67.222.222 -Type A).IPAddress returns your public IPv4 address through the OpenDNS resolver.
How do I get the IP address of a remote computer in PowerShell?
Resolve-DnsName -Name PC01 -Type A shows the address that DNS has on record for the computer. To read the computer’s own settings, run Invoke-Command -ComputerName PC01 -ScriptBlock { Get-NetIPAddress -AddressFamily IPv4 }, or Get-NetIPAddress -AddressFamily IPv4 -CimSession PC01. Both need PowerShell remoting (WinRM) enabled on the target and an account in its Administrators or Remote Management Users group.
How do I get the hostname from an IP address in PowerShell?
Run Resolve-DnsName -Name 8.8.8.8 -Type PTR and read the NameHost column, which shows dns.google for that address. [System.Net.Dns]::GetHostEntry(‘8.8.8.8’).HostName returns the same name and also works in PowerShell 7 on macOS and Linux. A reverse lookup finds a name only if the address has a PTR record, which many local devices and IPv6 addresses don’t have.
Look up IP addresses in your code
iplookupapi returns the location, provider, time zone and security flags of any IP address as JSON. The free plan includes 150 requests a month.
curl "https://api.iplookupapi.com/v1/info?ip=8.8.8.8" \
-H "apikey: YOUR_API_KEY"