-1

I am hoping to validate URLs similar to what can be done for file system and registry paths with Test-Path.

But, of course, Test-Path doesn't work on a URL, and I have been unable to find a way to do this in PowerShell.

I can use Invoke-WebRequest, but as far as I can tell there is no validation, I can get a return code of 200 if it's found, or 404 if it's not.

The only exception being an invalid host name, like host,com, which has me wondering:

  • Other than an invalid host name, IS there such a thing as an invalid URL?

  • Or is it basically any character valid in a URL path once the port and host are properly defined?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Gordon
  • 6,257
  • 6
  • 36
  • 89

2 Answers2

1

vonPryz and iRon have provided the crucial pointers:

You can use the System.Uri.IsWellFormedUriString method to test if a URI (URL) is well-formed, i.e. formally valid (irrespective of whether the domain exists, is reachable, the path exists, ...).

To additionally ensure that the given URI is limited to specific URI schemes, such as http:// and https://, you can do the following:

$uri = 'https://example.org/foo?a=b&c=d%20e'

[uri]::IsWellFormedUriString($uri, 'Absolute') -and ([uri] $uri).Scheme -in 'http', 'https'

Note that the given URI must already contain reserved characters in escaped form in order to be considered well-formed; e.g, spaces must be encoded as %20, as in the example above, which the System.Uri.EscapeDataString method can perform for the constituent (non-syntactic) parts of a URI (e.g. [uri]::EscapeDataString('a b'))

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I think you can approach this in a different way.

  1. Check if the URL format is correct by using Regular Expression
  2. Resolve the IP address behind that url, this would let you know if there is something behind that address.

Check the example bellow :

#1 URL format validation

#input the URL here
$urlInput = 'www.google.com'

#This is the regex pattern you can use to validate the format - reference : https://www.regextester.com/94502
$regEx="^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$"

if($urlInput -match $regEx){
    Write-Host "$urlInput is a Valid URL!"

    #2 is there a server behind this url
    try{
        Resolve-DnsName -Name $urlInput -ErrorAction Stop
    }catch{
        if($_ -like '*DNS name does not exist*'){
            Write-Host "No DNS record for the following URL : $urlInput"
        }else{
            Write-Output $_
        }
    }
}
else{
    Write-Host "Invalide URL - $urlInput "
}

P.S. I have used the following expression - https://www.regextester.com/94502 , you can play with it to match your use case.

Vasil Nikolov
  • 667
  • 6
  • 16