Is my -notlike string comparison syntax correct??

Welcome Forums General PowerShell Q&A Is my -notlike string comparison syntax correct??

Viewing 7 reply threads
  • Author
    Posts
    • #186092
      Participant
      Topics: 25
      Replies: 70
      Points: 45
      Rank: Member

      What is wrong with the syntax in this line??

      I am working with registry keys containing several strings ending in -500 pertaining to registry values I need to keep, retain, such as S-1-5-21-980708528-2451121298-916178342-500 and S-1-5-21-980708528-2451121298-916178342-500, which end in -500, and these are different within each server I am working with, but

      $i is part of a registry array containing SIDs like the ones above.

      If ($i -notlike '*-500')

      does not filter them out. At least one of the two registry values ending in -500 is not filtered out with this statement, the first one in the list.

      Is my -notlike syntax incorrect?? Single/double quotes seems not to matter.

      There's five servers involved so I'd better off using -notlike if I can.

      Thank you, Tom

    • #186095
      Participant
      Topics: 25
      Replies: 70
      Points: 45
      Rank: Member

      I also tried

      If ($i -notmatch "-500")

      This is the output I get every time, first several lines:

      S-1-5-21-980708528-2451121298-916178342-500 — this should not be returned
      S-1-5-21-3536713821-777046094-417734180-3426
      S-1-5-21-3536713821-777046094-417734180-5666
      S-1-5-21-3536713821-777046094-417734180-3712

      I am basically doing a string comparison, if the string contains *-500 at the end where * is any preceding characters, I don't want it.

    • #186107
      Participant
      Topics: 10
      Replies: 117
      Points: 457
      Helping Hand
      Rank: Contributor

      If the object in $i is a string, just correct your -match to the following:

      # Output matching '-500'
      if ($i -match '-500$') {
          ...
      }
      
      # Output not matching '-500'
      if ($i -notmatch '-500$') {
          ...
      }

      The $ in regex indicates end of line/string, which will match only -500 at the end. Something like '-5000' will not be returned.

      Using *-500 doesn't work because * means "0 or more instances of the preceding regex token" and nothing is specified before * in your code, and not really required in your case anyway.

      Posting your code (not just a line) gives better insight and speeds up getting a solution 🙂

    • #186110
      Participant
      Topics: 25
      Replies: 70
      Points: 45
      Rank: Member

      I read about the $ at the end but didn't realize it applied to my code.

      I also didn't know not to include the *, I thought I needed it.

      Thank you for explaining!! 🙂

    • #186128
      Participant
      Topics: 25
      Replies: 70
      Points: 45
      Rank: Member

      Here is the code as suggested to put here:

      `$RemoteKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::'LocalMachine',$targethost)`
      `$regPath = “SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache\PurgeAtNextLogoff\”`
      `$regkey = $RemoteKey.OpenSubKey($regPath,$true)`
      `$regkey.GetValueNames()`
      `$array = $regkey.GetValueNames()`
      `Foreach($i in $array)`
      `{`
      `# If ($i -ne “S-1-5-21-375116633-3803594802-1557413134-500”)`
      `If ($i -notmatch '-500$') `
      `{`
      `Write-host $i`
      `}`
      `}`

      Regardless whether I use -match or -notmatch, the whole list of values appears, which is not the desired result. I'll have to do things manually until I can get this code working, at which point I will replace write-host with RegKey.DeleteValue().

      The -notmatch is the key and it does not work?? Why??

      I'm sure there's better ways to write all this but first I need to reliably exclude all values ending with -500. 🙂

      I have since discovered this code which I got elsewhere on this forum does not really work for what I need, it always returns all the values no matter what I do.

      I need to find code that will return all the values except the -500 ones, I know get-propertyvalue in PS 5.x will do it, but not how to do it.

      Thank you, Tom

    • #186152
      Participant
      Topics: 1
      Replies: 1639
      Points: 3,104
      Helping Hand
      Rank: Community Hero

      May I suggest again an approach a little more "Powershelly"? 😉

      $Path = 'REGISTRY::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache\PurgeAtNextLogoff'
      (Get-Item -Path $Path).GetValueNames() |
           Where-Object {$_ -notmatch '-500$|-5666$'} |
              ForEach-Object{
                  $_
                  #Remove-ItemProperty -Path $Path -Name $_
              }

      As you can see I "filtered" two of the values by specifying two regex patterns. (if you're satisfied you can remove the comment before the Remove-ItemProperty)

      Edit #1: I'd recommend not to use code you don't understand. For beginners it's easier to stay with the pure Powershell syntax if possible. dotNet code can be tricky sometimes and can produce unexpected results for inexperienced scripters.

      Edit #2: You may read more about regular expressions here and here.

    • #186104
      Participant
      Topics: 1
      Replies: 2
      Points: 23
      Rank: Member

      Try it this way. This way it will look for everything before  -500.

       

      If ($i -notmatch "*-500")

    • #186580
      Participant
      Topics: 0
      Replies: 1
      Points: 7
      Rank: Member

      great info i love it

Viewing 7 reply threads
  • You must be logged in to reply to this topic.