How To Get IP Address In PowerShell (Private & Public).
Whenever your laptop is linked to a network or the Internet, your device may have two IP addresses—the general public IP handle and the non-public IP handle.
All gadgets on the Internet use your public IP for communication, whereas the non-public IP is to your native network solely. In reality, when most use the time period “IP address,” they really check with a non-public IP handle.
You can discover out your laptop’s non-public and public IP by means of PowerShell in several methods. I will even present how to assign the IP handle to a variable, which is helpful if you’re writing a PowerShell script.
How to Get Private IP Address in PowerShell?
You can use varied Net TCP/IP-specific and device management-related cmdlets on PowerShell to get the non-public IP handle. These cmdlets test your network and network adapter configurations, together with this knowledge.
Also, whereas all these cmdlets can help you get the IP handle, they present different parameters as effectively. Since the kind of parameters differs between the cmdlets, you would possibly want one over the opposite relying in your scenario.
Using Get-NetIPAddress
The normal solution to show the IP handle in PowerShell is by utilizing the Get-NetIPAddress
cmdlet, which reveals the IP handle configuration.
This cmdlet lists the IP configuration of all bodily and digital networks in your system. So, you could mix it with an extra cmdlet to slim down the consequence to the present network.
Using Get-NetIPConfiguration
Another approach you may decide the present IP handle is by checking the present network configuration by way of Get-NetIPConfiguration
.
As with the above case, Get-NetIPConfiguration
additionally lists out all network configurations. So, it’s higher to make use of the Where-Object
cmdlet to listing the present network particulars solely.
Using IPConfig
IPConfig
is the Command Prompt equal of Get-NetIPConfiguration
. Since PowerShell additionally lets you use Command Prompt instructions, you should utilize IPConfig as effectively.
- Here, you may kind
ipconfig
and press Enter after which search for the IPv4 handle in your network (whose Default Gateway is just not empty). - Or you may enter
((ipconfig | findstr [0-9]..)[0]).Split()[-1]
to solely show the IP handle. You may straight assign it to a variable, for e.g.,$MyIP=((ipconfig | findstr [0-9]..)[0]).Split()[-1]
Using Test-NetConnection
Test-NetConnection
is the most recent alternative for ping and traceroute features in PowerShell. This cmdlet additionally shows the IP handle of your laptop whereas performing a ping take a look at.
- You can merely enter
Test-NetConnection
or Test-NetReference to any server likeTest-NetConnection google.com
orTest-NetConnection 8.8.8.8
. You will see the IPv4 handle underneath SourceAddress. - If you need to get simply the IP handle, you may enter
(Test-NetConnection).SourceAddress.IPAddress
. And you may equally assign it to a price. For instance,$MyIP=(Test-NetConnection).SourceAddress.IPAddress
Using Get-WmiObject
Your laptop’s network adapter configurations come underneath the Windows Management Instrumentation (WMI) courses. So, you may straight get such data by utilizing the Get-WmiObject cmdlet. In reality, Get-NetIPAddress
and Get-NetIPConfig
use the knowledge within the WMI class to show their outcomes as effectively.
The WMI class you want is Win32_NetworkAdapterConfiduration. And for the reason that Get-WmiObject
cmdlet will show all adapter configurations, you’ll need the Where-Object
cmdlet to solely show the present network’s particulars.
Using Get-CimInstance
Get-CimInstance
is the improved model of Get-WMIObject
so it’s higher to make use of this cmdlet for many circumstances. But since the results of each cmdlets on this regard is analogous, you may select both of the 2.
However, Get-CimInstance
shows the info in a tabular format by default, so could have to append further cmdlets to get the IP handle.
- You can use the
Format-List
cmdlet to vary the formatting to an inventory kind much likeGet-WMIObject
.
For instance,(Get-CimInstance -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}) | Format-List
- If you need to get simply the IPv4 handle or assign it to a variable, you don’t want to make use of 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 need to know your public IP handle by way of PowerShell, you could use cmdlets that ship HTTP or HTTPS knowledge to some particular third-party servers.
The easiest method to take action is by utilizing Invoke-WebRequest or Invoke-RestMethod cmdlets. And you should utilize the servers like https://api.ipify.org/, https://ident.me, https://ifconfig.me/ip, https://ipinfo.io/ip, and so forth.
Using Invoke-WebRequest
- Enter the cmdlet,
Invoke-WebRequest -uri “https://api.ipify.org/”
and test the worth of Content to get the general public IP. - You can use another servers as we talked about earlier than instead of
https://api.ipify.org/
in the identical approach. - If you need simply the general public IP handle, you should utilize the interval (.) operator.
(Invoke-WebRequest -uri “https://api.ipify.org/”).Content
and you may assign it to any variable you need.
Using Invoke-RestMethod
- Enter the cmdlet,
Invoke-RestMethod -uri (“https://api.ipify.org/”)
to get the general public IP. - You can equally substitute the uniform useful resource identifier (URI) with another server talked about earlier in addition to straight assign it to a variable.
Check out more article on – How-To tutorial and latest highlights on – Technical News