6

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 ifconfig.me/ip).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 ifconfig.me/ip).Content.Trim())
$IPCHK | Out-FIle 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt'

IF($PreviousIP -eq ((Invoke-WebRequest ifconfig.me/ip).Content.Trim()))
    {
        $PreviousIP
        }
ELSE {
       ##SEND EMAIL SCRIPT
        IPV #RUN CHECK IP COMMAND AGAIN.
}
2
  • Can you explain why you need this info? Trying to find a server's external IP address is really tricky. Aug 28, 2014 at 3:33
  • 2
    I want to run this script which will check if my external IP changed. if it does then it will send me notification on my iPhone.
    – user206168
    Aug 28, 2014 at 11:31

7 Answers 7

19

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

(Invoke-WebRequest ifconfig.me/ip).Content

as is. Instead, you could use this:

(Invoke-WebRequest ifconfig.me/ip).Content.Trim()

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

5
  • It still doesn't compare it correctly. Plz check my posted script, if i run this script it still has extra line. thanks
    – user206168
    Aug 27, 2014 at 20:13
  • Trim() the output of Get-Content, just to be sure. Also, use your $CurrentIP variable. ;)
    – Daniel B
    Aug 27, 2014 at 20:17
  • so it would be Get-Content.Trim()?
    – user206168
    Aug 27, 2014 at 20:19
  • 1
    No, it works like Invoke-WebRequest. You put parentheses around it and the .Trim() after that. But I recommend posting a new question, because all this deviates too much from this question’s original focus.
    – Daniel B
    Aug 27, 2014 at 20:21
  • Plz See My solution.
    – user206168
    Aug 28, 2014 at 15:48
6

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
  • Sorry? Why did you add a comment with only the command I suggested?
    – Zoredache
    Aug 27, 2014 at 20:19
  • Sorry Pasted wrong Resolve-DnsName : The term 'Resolve-DnsName' is not recognized as the name of a cmdlet, function,
    – user206168
    Aug 27, 2014 at 20:23
  • Ah, I think it is one of the Win 8+ Commandlets. I am guessing you are on Windows 7?
    – Zoredache
    Aug 27, 2014 at 20:24
  • No I am using Windows 8.
    – user206168
    Aug 27, 2014 at 20:26
  • 1
    This will not work if direct outbound connections to port 53 are blocked which might happen if network administrator is enforcing DNS encryption for example. In that case it will timeout and then return the IP address of resolver1.opendns.com itself. Sep 30, 2020 at 15:08
3

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

(Invoke-WebRequest -UseBasicParsing ifconfig.me/ip).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
  • Thanks Yury! I did get that error, and it went away after using this flag as you've suggested.
    – Shiva
    Jun 2, 2020 at 2:44
1

Short answer

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

Invoke-WebRequest -Uri "http://icanhazip.com" | 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 "http://icanhazip.com" | 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.

1

I expanded on this to tidy it up a bit and add Telegram functionality.

I also removed the function commands as they weren't needed in my use case.

<# Lets set our file paths #>

$Last_IP_Path = "PATH\FILE.TXT"
$Current_IP_Path = "PATH\FILE.TXT"

<# Now we need to load our Last WAN IP #>
$PreviousIP = Get-Content $Last_IP_Path | SELECT -First 1   #ONLY Selects First Line.

<# Lets work out what our current WAN IP #>
$CurrentIP = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim()) | Out-File $Current_IP_Path

<# Now that we know our current WAN IP lets make it usable #>
$CURRENTWANIP = Get-Content $Current_IP_Path | SELECT -First 1   #ONLY Selects First Line.
    
    <# Lets Compare our IP DATA #>
    IF
    (
        $PreviousIP -eq $CURRENTWANIP
    )
    {
        <# Just going to echo because we don't have anything better to do #>
        $PreviousIP
    }
        <# Now we have something to do #>
        Else
    {
        <# Lets tell TELEGRAM our new WAN IP #> 
        $MyToken = "TELEGRAMTOKENHERE"
        $chatID = "TELEGRAMCHATIDHERE"
        $Message = "New WAN IP: $CURRENTWANIP"
        $Response = Invoke-RestMethod -Uri "https://api.telegram.org/bot$($MyToken)/sendMessage?chat_id=$($chatID)&text=$($Message)"
        
        <# Lets update our Sauce files for the next run #> 
        copy-item $Current_IP_Path $Last_IP_Path -force 
    }
0

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 ifconfig.me/ip).Content)
$IPCHK.TRIM() # Just Like tht.
$IPCHK > 'CHKIP.txt'
}
$CurrentIP = ((Invoke-WebRequest ifconfig.me/ip).Content.Trim())
$PreviousIP = Get-Content 'CHKIP.txt' | SELECT -First 1   #ONLY Selects First Line.

IF($PreviousIP -eq ((Invoke-WebRequest ifconfig.me/ip).Content.Trim()))
    {
        $PreviousIP
        }
ELSE {
        #DO YOUR STUFF :))
        IPV #RUN CHECK IP COMMAND AGAIN.
}

Hope it helps.

1
  • You're still not using your $CurrentIP variable. :P
    – Daniel B
    Aug 28, 2014 at 16:31
0

I'm using this simple line command:

curl ifconfig.me
1
  • 1
    Could you please edit your answer to give an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.
    – DavidPostill
    Nov 6, 2022 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .