Reputation: 135
I am checking a registry key using get-childitem
with the -recurse
option. I am piping that into Get-ItemProperty
, where I'm looking for a specific value of a string value. I am then selecting that object. I want to further enhance my script to add an If
statement. I only want to do something If one word in my string contains a capital letter. The trick is, only that specific word and no other words.
The value of my string value looks like this: Https:\\Blah.blah.com
So in the above, only the if
statement to be true if "https" contains a capital. Above, it does contain a capital. If however the value returned is something like https:\\CapitalLettersHereareOK.com
. Then the If
statement should return false.
I can't use -cmatch
because, while it works, it's matching the entire string. As outlined above, some of the letters in the string could be capital, and that is ok. I just need to know if "HTTPS" contains any capital letters.
I'm no good at Regex and I'm pretty sure that is needed here. See code below:
$GetWebValue = Get-ChildItem -Path 'HKLM:\Software\MySoftwareKey' -Recurse -ErrorAction SilentlyContinue | Get-ItemProperty -Name Web -ErrorAction SilentlyContinue | Select-object -expandproperty Web
Upvotes: 2
Views: 1304
Reputation: 437618
The key is to isolate the protocol name (everything before :
) and test only it for the presence of uppercase characters; e.g.:
PS> ('Https:\\Blah.blah.com' -split ':')[0] -cmatch '\p{Lu}'
True # ditto for 'httpS:\\...', 'hTtps:\\...', 'HTTPS:\\...', ...
PS> ('https:\\Blah.blah.com' -split ':')[0] -cmatch '\p{Lu}'
False
(... -split ':')[0]
extracts the first :
based token from the LHS string, i.e., the protocol name.
-cmatch
case-sensitively matches regex \p{Lu}
(an uppercase letter) anywhere in that protocol name.
a
through z
can be part of a protocol name, regex [A-Z]
would suffice; by contrast, \p{Lu}
matches any uppercase Unicode character classified as a letter.In the context of your command:
Get-ChildItem -Path 'HKLM:\Software\MySoftwareKey' -Recurse -ErrorAction SilentlyContinue |
Get-ItemProperty Web -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Web |
Where-Object { ($_ -split ':')[0] -cmatch '\p{Lu}' }
Note that while you should be able to use Get-ItemPropertyValue Web
(PSv5+) instead of the roundabout Get-ItemProperty Web | Select-Object -ExpandProperty Web
, so as to directly extract just the data from each matching registry value, this is not an option as of this writing, due to a known bug.
Upvotes: 3