Introducing new blog series - Bag of toys

PS C#

Introduction

I have many PowerShell scripts and functions lying around, some are preloaded in my profile, some I just use from time to time and can be found in PowerShell’s folder on one of my computers.

Every time I am faced with a computer task (or a whim) that takes more then 2 minutes to accomplish by hand – I turn to PowerShell (or VIM console for text manipulations, but that’s another story).

So, I decided to share these tiny-but-useful scripts and functions by launching a blog series named “Bag of toys”.

Script #1 – Get your external IP

This little one-liner tells you your external IP address, the one exposed to the world, not the one you got from your ISP (which is much easier to find out, using IpConfig util).

 (New-Object net.webclient).downloadstring("https://checkip.dyndns.com") -replace "[^\d\.]"

While this is quite self-explanatory to most PowerShell users, let’s just do a quick run-through to make sure we covered all the bases. So, from left to right:

  1. We create a new WebClient object.
  2. And call its DownloadString method to get a string response from a web server.
  3. DynDns service is kind enough to expose a rather straight-forward url for getting our IP.
  4. If you try to execute this URL in your browser, you’ll get a plain-text page with the text - “Current IP Address: your-ip-address
  5. We don’t want the text, just the IP itself, so we use a -replace to remove any character that is neither a digit (\d) nor a dot.