Is my -notlike string comparison syntax correct??

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

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

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.

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


If ($i -notmatch “*-500”)

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 :slight_smile:

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!! :slight_smile:

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)`
`$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. :slight_smile:

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

May I suggest again an approach a little more “Powershelly”? :wink:

$Path = ‘REGISTRY::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache\PurgeAtNextLogoff’
(Get-Item -Path $Path).GetValueNames() |
Where-Object {$_ -notmatch ‘-500$|-5666$’} |
#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.

great info i love it