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.
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.
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
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.
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.