Whenever your computer is connected to a network or the Internet, your device will have two IP addresses—the public IP address and the private IP address.
All devices on the Internet use your public IP for communication, whereas the private IP is for your local network only. In fact, when most use the term “IP address,” they actually refer to a private IP address.
You can find out your computer’s private and public IP through PowerShell in different ways. I will also show how to assign the IP address to a variable, which is useful if you are writing a PowerShell script.
How to Get Private IP Address in PowerShell?
You can use various Net TCP/IP-specific and device management-related cmdlets on PowerShell to get the private IP address. These cmdlets check your network and network adapter configurations, including this data.
Also, while all these cmdlets allow you to get the IP address, they show other parameters as well. Since the type of parameters differs between the cmdlets, you might prefer one over the other depending on your situation.
Using Get-NetIPAddress
The standard way to display the IP address in PowerShell is by using the Get-NetIPAddress
cmdlet, which shows the IP address configuration.
This cmdlet lists the IP configuration of all physical and virtual networks on your system. So, you need to combine it with an additional cmdlet to narrow down the result to the current network.
- Here, you can use the
Where-Object
cmdlet to only list the network IP address details with ‘Preferred’ Address State and the Valid Lifetime that does not go beyond 24 hrs. The combined cmdlet is:Get-NetIPAddress | Where-Object {$_.AddressState -eq "Preferred" -and $_.ValidLifetime -lt "24:00:00"}
- You can also use the period operator (.) to get only one property (in this case, the IPAddress) from this data.
(Get-NetIPAddress | Where-Object {$_.AddressState -eq "Preferred" -and $_.ValidLifetime -lt "24:00:00"}).IPAddress
- Additionally, you can use the last command to store the IP address in a variable. You need to enter the cmdlet:
$MyIP=(Get-NetIPAddress | Where-Object {$_.AddressState -eq "Preferred" -and $_.ValidLifetime -lt "24:00:00"}).IPAddress
while replacing$MyIP
with your own variable.
Using Get-NetIPConfiguration
Another way you can determine the current IP address is by checking the current network configuration via Get-NetIPConfiguration
.
As with the above case, Get-NetIPConfiguration
also lists out all network configurations. So, it’s better to use the Where-Object
cmdlet to list the current network details only.
- You need to select the network which has an assigned IPv4 Default Gateway (the value should not be empty) and whose adapter is not currently disconnected. The cmdlet is:
Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}
- To only get the IP address, you can use the cmdlet below
(Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.IPAddress
- It is similarly possible to assign it to a value. For instance,
$MyIP=(Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.IPAddress
Using IPConfig
IPConfig
is the Command Prompt equivalent of Get-NetIPConfiguration
. Since PowerShell also allows you to use Command Prompt commands, you can use IPConfig as well.
- Here, you can type
ipconfig
and press Enter and then look for the IPv4 address on your network (whose Default Gateway is not empty). - Or you can enter
((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
to only display the IP address. You can also directly assign it to a variable, for e.g.,$MyIP=((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
Using Test-NetConnection
Test-NetConnection
is the latest replacement for ping and traceroute functions in PowerShell. This cmdlet also displays the IP address of your computer while performing a ping test.
- You can simply enter
Test-NetConnection
or Test-NetConnection with any server likeTest-NetConnection google.com
orTest-NetConnection 8.8.8.8
. You will see the IPv4 address under SourceAddress. - If you want to get just the IP address, you can enter
(Test-NetConnection).SourceAddress.IPAddress
. And you can similarly assign it to a value. For instance,$MyIP=(Test-NetConnection).SourceAddress.IPAddress
Using Get-WmiObject
Your computer’s network adapter configurations come under the Windows Management Instrumentation (WMI) classes. So, you can directly get such information by using the Get-WmiObject cmdlet. In fact, Get-NetIPAddress
and Get-NetIPConfig
use the information in the WMI class to display their results as well.
The WMI class you need is Win32_NetworkAdapterConfiduration. And since the Get-WmiObject
cmdlet will display all adapter configurations, you will need the Where-Object
cmdlet to only display the current network’s details.
- You can list the Network Adapter Configuration where the DHCP is enabled and Default Gateway is not empty using the cmdlet below:
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null})
- Now, you can use the period operator to display the IPAddress property only.
(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress
- The IPAddress property shows both the IPv4 address and the Link-Local IPv6 Address. If you need to get only the IPv4 address, you can add
Select-Object -First 1
cmdlet to only get the first value, i.e., IPv4 address.(Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
- And similar to the other methods, you can directly assign it to a variable. For instance,
$MyIP = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
Using Get-CimInstance
Get-CimInstance
is the improved version of Get-WMIObject
so it’s better to use this cmdlet for most cases. But since the result of both cmdlets in this regard is similar, you can choose either of the two.
However, Get-CimInstance
displays the data in a tabular format by default, so may need to append additional cmdlets to get the IP address.
- You can use the
Format-List
cmdlet to change the formatting to a list form similar toGet-WMIObject
.
For instance,(Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}) | Format-List
- If you want to get just the IPv4 address or assign it to a variable, you don’t need to use Format-List.
(Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
How to Get Public IP Address in PowerShell?
If you want to know your public IP address via PowerShell, you need to use cmdlets that send HTTP or HTTPS data to some specific third-party servers.
The easiest way to do so is by using Invoke-WebRequest or Invoke-RestMethod cmdlets. And you can use the servers like https://api.ipify.org/, https://ident.me, https://ifconfig.me/ip, https://ipinfo.io/ip, and so on.
Using Invoke-WebRequest
- Enter the cmdlet,
Invoke-WebRequest -uri “https://api.ipify.org/”
and check the value of Content to get the public IP. - You can use any other servers as we mentioned before in place of
https://api.ipify.org/
in the same way. - If you want just the public IP address, you can use the period (.) operator.
(Invoke-WebRequest -uri “https://api.ipify.org/”).Content
and you can assign it to any variable you want.
Using Invoke-RestMethod
- Enter the cmdlet,
Invoke-RestMethod -uri (“https://api.ipify.org/”)
to get the public IP. - You can similarly replace the uniform resource identifier (URI) with any other server mentioned earlier as well as directly assign it to a variable.