Home > other >  Small problem with (if) command in powershell
Small problem with (if) command in powershell

Time:11-09

First, let me show you the code

$url = Read-Host 'URL'
if ( $url -gt '.'){
    msg * "The title does not contain(.)"
}

The code should alert you that the site does not have a dot

But either way the code alerts me

The code does not give me any error

CodePudding user response:

The -gt operator does a case-insensitive lexical comparison, and where non-letters are involved, it uses case-folded Unicode ordering. The . is ordered before any of the letters, so virtually any URL, with or without a dot, will be lexically greater than the single dot you are comparing against.

If you want to test whether a string contains a dot, you should most likely be using the -match or -notmatch operators; note that this uses regular expressions.

$url = Read-Host 'URL'
if ( $url -notmatch '\.'){
    msg * "The title does not contain(.)"
}
  • Related