Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: What’s My IP

Posted on April 3, 2015

Today's Friday Fun is a "quick and dirty" solution to the question, "What is my public IP address?" There are plenty of web sites and services you can visit that will display that piece of information. Naturally I want an easy way to get this in PowerShell. Recently I came across a reference to a web site that simply displays your IP address. To see for yourself go to http://icanhazip.com/.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Pretty cool.

This means I can use PowerShell.
getmyip

The content property is all I need so here's a one-liner.

(invoke-webrequest http://icanhazip.com/ -UseBasicParsing -DisableKeepAlive).content.trim()

Of course that's a lot to type so I put together a simple function.

Function Get-MyPublicIP {
[cmdletbinding()]

Param()

$paramHash = @{
 uri = 'http://icanhazip.com/'
 DisableKeepAlive = $True
 UseBasicParsing = $True
 ErrorAction = 'Stop'
}
Try {
    $request = Invoke-WebRequest @paramHash
    $request.Content.Trim()
}
Catch {
    Throw $_
}

} #end function

Now I can easily get my public IP.
getmyip2

Nothing fancy, just getting the job done. Enjoy.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Friday Fun: What’s My IP”

  1. Anton says:
    April 9, 2015 at 5:48 am

    Windows 8.1 and WS2012R2 have built-in Test-NetConnection for this. But for older OS, this is nice. Thank you!

    1. B Josephs says:
      April 15, 2015 at 3:57 pm

      @ Anton – Am I missing something?
      Test-NetConnection doesn’t give my public ip unless, of course, I tease it out of a TraceRoute:
      PS C:> (Test-NetConnection -TraceRoute).traceroute[0]
      That being the case, the script is much faster (granted, we are talking milliseconds here. :))

      @ Jeffery – thanks!

      1. Jeffery Hicks says:
        April 15, 2015 at 4:08 pm

        And it may not always do that. When I try it, I never see my public IP.

  2. John says:
    April 16, 2015 at 8:15 am

    Has icanhazip been around for a long time?

    1. Jeffery Hicks says:
      April 16, 2015 at 8:52 am

      I think it has. He has other services as well.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d