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?
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')
)