0

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

4
  • 1
    What do you mean with "has no dot"? Like "hostname" vs. "hostname.domain"? The -gt operator seems a little far off for string comparison (what do you expect from $url "greater than" '.'?). Finally, the line msg * "..." also looks fishy syntax wise. Commented Nov 8, 2021 at 12:58
  • 2
    @Christian.K - The msg * ... line is legit; it's invoking the msg.exe utility, which simply pops up a dialog box with the message text and an OK button. Commented Nov 8, 2021 at 13:00
  • @Jeff Thanks! To add to my previous comment, I would have expected something along the lines of if (!$url.Contains('.')) { msg * "Does not contain a dot" }. Commented Nov 8, 2021 at 13:00
  • ! $url.Contains('.') is propably the most efficient way. For completeness, using PowerShell operators: $url -notlike '*.*' Commented Nov 8, 2021 at 13:07

1 Answer 1

2

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(.)"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, you worked with me, but Can you make the message appear if (.) is not found twice
I would have to verify the regular expression, and I don't quite have time right now, but I think you would want to match (or notmatch) against \..*\.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.