Figuring out your public IP address with PowerShell

Sometimes, you need to know your public IP address because of… reasons. My particular reason was creating firewall rule to limit SSH only from my current public IP address, to a machine on the Internet. And how to do it?

You can always use free services like What Is My IP?, which shows you your public IP address in a nice form:

But there are also other ways – if you’re running Linux (or WSL) and do a Google search for the command that can help you, you’ll probably get this (

curl -s | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

And if you’re using Windows, PowerShell is here to help you! I like “oneliners”, even if they are not always easy to read:

($($(Invoke-WebRequest -Uri "").ParsedHtml.getElementsByTagName('body')).innerText).TrimStart("Current IP Address: ")

I’m sure that my friend Aleksandar (PowerShell guru & Microsoft MVP) has a better way, but for me, this works just fine. 🙂

Hope it helps!


Leave a Comment.