#PSTip Test if a URL is absolute or not

The System.Uri class in .NET provides a way to validate if a URL is an absolute URL or not. This can be quite handy when your script deals with downloading content from web pages and there is a need to validate the specified URL.

The IsWellFormedUriString method provides this capability.


The UriKind enumeration has three values–Absolute, Relative, and RelativeOrAbsolute. The IsWellFormedUriString() method returns a Boolean value based on the match.

I wrapped this in a small function called Test-Url:

Function Test-Url {

    param (
        [String] $Url

    Process {
        if ([system.uri]::IsWellFormedUriString($Url,[System.UriKind]::Absolute)) {
        } else {

And, here is a practical example where I am using this function.

$doc = Invoke-WebRequest -Uri 'https://www.python.org/downloads'
foreach ($href in ($doc.links.href -ne '')) {
    if (Test-Url -Url $href) {
Filed in: Columns, Tips and Tricks Tags: ,

One Response to "#PSTip Test if a URL is absolute or not"

  1. sai venkatesh says:

    ravi this is venkat from coimbatoe i have a doubt regarding the url availability. i have 2 urls thats need to be monitor atleast 10 mins once incase of any down the mail should trigger to my id which i gave mob no is iptional
    how to implemetn this??

    kindly update us on the same

Leave a Reply

Submit Comment

© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: