To make sure user input is a valid URL, you can use the System.URI type. Try to convert the raw string into this type. If it works, the string is a valid URI. You can then further examine the converted result to limit validation to only http/https URLs:
function isURI($address) { ($address -as [System.URI]).AbsoluteURI -ne $null}function isURIWeb($address) { $uri = $address -as [System.URI] $uri.AbsoluteURI -ne $null -and $uri.Scheme -match '[http|https]'}isURI('http://www.powershell.com')isURI('test')isURI($null)isURI('zzz://zumsel.zum')"-" * 50isURIWeb('http://www.powershell.com')isURIWeb('test')isURIWeb($null)isURIWeb('zzz://zumsel.zum')
My machine still has this error. Can anyone explain it to me?
"-" * 50isURIWeb('http://www.powershell.com')isURIWeb('test')isURIWeb($null)isURIWeb('zzz://zumsel.zum')slope unblocked
Thanks! Yet, small edit: Remove the brackets from the regular expression. With the brackets means that you accept any character, so "htp" is also a valid scheme.