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 "/") 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.
> $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
> $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
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.
Need more than just this article? We’re available to consult.
You might also want to check out these related articles: