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 toget 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 theGet-NetIPAddresscmdlet, 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.
Using Get-NetIPConfiguration
Another way you can determine the current IP address is by checking the current network configuration viaGet-NetIPConfiguration.
As with the above case,Get-NetIPConfigurationalso lists out all network configurations. So, it’s better to use theWhere-Objectcmdlet to list the current network details only.
Using IPConfig
IPConfigis the Command Prompt equivalent ofGet-NetIPConfiguration. Since PowerShell also allows you to use Command Prompt commands, you canuse IPConfigas well.
Using Test-NetConnection
Test-NetConnectionis 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.
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-NetIPAddressandGet-NetIPConfiguse the information in the WMI class to display their results as well.
The WMI class you need isWin32_NetworkAdapterConfiduration. And since theGet-WmiObjectcmdlet will display all adapter configurations, you will need theWhere-Objectcmdlet to only display the current network’s details.
Using Get-CimInstance
Get-CimInstanceis the improved version ofGet-WMIObjectso it’s better to use this cmdlet for most cases. But since the result of both cmdlets in this regard is similar, you’re able to choose either of the two.
However,Get-CimInstancedisplays the data in a tabular format by default, so may need to append additional cmdlets to get the IP address.
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 likehttps://api.ipify.org/,https://ident.me,https://ifconfig.me/ip,https://ipinfo.io/ip, and so on.