PowerShell: Splitting URL Strings

Sometimes, you have a full URL string but you only need one of its components. PowerShell makes it easy to split them out. Suppose we need the name of the ISO file from here:

$UbuntuIsoUrlString = "https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-desktop-amd64.iso"

We want the substring ubuntu-20.04.3-desktop-amd64.iso. We could use the split operator:

> $($UbuntuIsoUrlString -split "/")[-1]                                                       
ubuntu-20.04.3-desktop-amd64.iso

This divides the string into the components between the / characters and stores each one in an array. The [-1] index selects the last element of that array. This works for the ISO name, but it fails in other cases. Suppose we need the scheme:

> $($UbuntuIsoUrlString -split "/")[0] 
https:

The scheme is https, but we got https: (with a colon). We were splitting specifically on / (slash) characters. The colon isn’t a slash, so split counted it as part of the first component. Split doesn’t understand URLs. It just divides the string whenever it sees the character we told it to split on. We could strip the colon off after we split, but there’s a better way.

.NET has a class that can be instantiated to represent URI objects. URLs are a type of URI. If we cast our string to a URI, we can use the properties defined by that class to get the scheme:

> $UbuntuIsoUri = [System.Uri]$UbuntuIsoUrlString
> $UbuntuIsoUri.Scheme
https

This class understands URLs. It knows the colon is a delimiter character, not part of the scheme. It excludes that character for us.

We can shorten this a bit with the URI type accelerator:

> $UbuntuIsoUri = [uri]$UbuntuIsoUrlString
> $UbuntuIsoUri.Scheme
https

If we want to get the ISO name from this object, we can use the Segments property:

> $UbuntuIsoUri.Segments[-1]
ubuntu-20.04.3-desktop-amd64.iso

Segments returns an array of all the path segments. We get the last one with the [-1] index.

Let’s make the whole operation a one-liner so it’s easy to copy/paste:

> ([uri]$UbuntuIsoUrlString).Segments[-1]
ubuntu-20.04.3-desktop-amd64.iso

That’s the Posh way to process URLs! Cast to a URI object, then read whatever data you need from that object’s properties. As always, PowerShell is all about objects.

Happy Automating!

Operating Ops

Need more than just this article? We’re available to consult.

You might also want to check out these related articles: