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!
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.
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.
Nothing fancy, just getting the job done. Enjoy.
Windows 8.1 and WS2012R2 have built-in Test-NetConnection for this. But for older OS, this is nice. Thank you!
@ 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!
And it may not always do that. When I try it, I never see my public IP.
Has icanhazip been around for a long time?
I think it has. He has other services as well.