Getting External IP via PowerShell

I would just like to Get External IP via Powershell Just IP address no header or anything.

I try many things such as

(Invoke-WebRequest ).Content

but it has this extra second line which doesn't do good for me.

I also tried.

(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE) | %{$_.ipaddress[0]}

But it doesn't work for me because I am behind the router.

Thanks.

Function IPV()
{
$IPCHK = ((Invoke-WebRequest ).Content.Trim())
$IPCHK | Out-FIle 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt'
IF($PreviousIP -eq ((Invoke-WebRequest ).Content.Trim())) { $PreviousIP }
ELSE { ##SEND EMAIL SCRIPT IPV #RUN CHECK IP COMMAND AGAIN.
}
2

5 Answers

You already have your answer. You just want to get rid of the additional line—nothing is forcing you to use

(Invoke-WebRequest ).Content

as is. Instead, you could use this:

(Invoke-WebRequest ).Content.Trim()

The String.Trim method “removes all leading and trailing white-space characters from the current String object.”

5

One method using OpenDNS.

With Resolve-DnsName CmdLet available in Powershell 4 in Windows 8.1 / Server 2012 or later

$(Resolve-DnsName -Name myip.opendns.com -Server 208.67.222.220).IPAddress

Or in earlier Windows versions with just plain nslookup

$my_ip = ((& "nslookup" "myip.opendns.com" "208.67.222.220") |select -last 2)[0].Trim("Address:").Trim()
6

Just one small (but important) addendum to Deniel's answer:

(Invoke-WebRequest -UseBasicParsing ).Content.Trim()

Otherwise, on some machines you may face the error like this:

Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

1

Short answer

For the PowerShell newbs (where .NET methods may look a bit scary), try the following (case insensitive) code:

Invoke-WebRequest -Uri "" | Select-Object -ExpandProperty "Content"

The above is the simplest (in my humble opinion) way to obtain one's external/public/WAN IP address via PowerShell (also known as "PoSh").

Long answer

This technique relies on a helpful website (there are several, in fact) that returns only the public IP address of the connection that you're connecting from.

We use the handy Invoke-WebRequest cmdlet, which gives us a bunch of info about the website. In this case though, we actually want the raw HTML of the webpage (which happens to be just your public IP address). The Select-Object cmdlet will allow us to do just that, and discard the rest of the information returned.

Having said that, as the other answerers have highlighted, this approach actually returns, not one, but two lines:

One line containing the IP address, and one line containing white space.

If this causes an issue (for simple requirements, such as printing the IP address to the screen, this is not likely to cause an issue), you will need to delve into the aforementioned .NET method world, ever so slightly.

Again, case insensitive:

( Invoke-WebRequest -Uri "" | Select-Object -ExpandProperty "Content" ).Trim( "" )

The Trim() method will "trim" anything matching the characters specified in between the double-quote marks (which are actually optional in this case) from the beginning or the end of the returned data (or string, technically speaking). Being that in this example there is actually nothing between the double-quote marks, PowerShell will interpret this as "white space" and trim anything from the start or end that's considered white space.

The initial pair of brackets is required when using .NET methods with PowerShell cmdlets, and the second pair of brackets is part of the method itself.

This combination of PowerShell cmdlets and .NET method leaves you with just the IP address you seek.

I was able to fix my problem by only selecting the first line from the text file. I was not successfuly able use TRIM and remove the empty line.

Using this link I did following.

Function IPV()
{
$IPCHK = ((Invoke-WebRequest ).Content)
$IPCHK.TRIM() # Just Like tht.
$IPCHK > 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt' | SELECT -First 1 #ONLY Selects First Line.
IF($PreviousIP -eq ((Invoke-WebRequest ).Content.Trim())) { $PreviousIP }
ELSE { #DO YOUR STUFF :)) IPV #RUN CHECK IP COMMAND AGAIN.
}

Hope it helps.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like