I created a code script for registry issues. Use RegRootReplace -Key $Key -ResultType 'registry' in order to not having any issue for key-not-found problems.
<#
# "REGISTRY::HKEY_CLASSES_ROOT" = "HKEY_CLASSES_ROOT" = "HKCR:"
# "REGISTRY::HKEY_CURRENT_CONFIG" = "HKEY_CURRENT_CONFIG" = "HKCC:"
# "REGISTRY::HKEY_USERS" = "HKEY_USERS" = "HKU:"
# "REGISTRY::HKEY_CURRENT_USER" = "HKEY_CURRENT_USER" = "HKCU:"
# "REGISTRY::HKEY_LOCAL_MACHINE" = "HKEY_LOCAL_MACHINE" = "HKLM:"
#>
function RegRootReplace {
<#
.EXAMPLE
RegRootReplace -Key $Key -ResultType $ResultType # $ResultType can be 'small', 'long' or 'registry'.
#>
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
$Key,
[Parameter(Mandatory = $false)]
[string[]]
$ResultType = 'small'
)
$Key = $Key -replace 'REGISTRY::', ''
switch ($ResultType) {
'small' {
$Key = $Key -replace 'HKEY_CLASSES_ROOT', 'HKCR:'
$Key = $Key -replace 'HKEY_CURRENT_CONFIG', 'HKCC:'
$Key = $Key -replace 'HKEY_USERS', 'HKU:'
$Key = $Key -replace 'HKEY_CURRENT_USER', 'HKCU:'
$Key = $Key -replace 'HKEY_LOCAL_MACHINE', 'HKLM:'
return $Key
}
'long' {
$Key = $Key -replace 'HKCR:', 'HKEY_CLASSES_ROOT'
$Key = $Key -replace 'HKCC:', 'HKEY_CURRENT_CONFIG'
$Key = $Key -replace 'HKU:', 'HKEY_USERS'
$Key = $Key -replace 'HKCU:', 'HKEY_CURRENT_USER'
$Key = $Key -replace 'HKLM:', 'HKEY_LOCAL_MACHINE'
return $Key
}
'registry' {
$Key = 'REGISTRY::' + $Key
$Key = $Key -replace 'HKCR:', 'HKEY_CLASSES_ROOT'
$Key = $Key -replace 'HKCC:', 'HKEY_CURRENT_CONFIG'
$Key = $Key -replace 'HKU:', 'HKEY_USERS'
$Key = $Key -replace 'HKCU:', 'HKEY_CURRENT_USER'
$Key = $Key -replace 'HKLM:', 'HKEY_LOCAL_MACHINE'
return $Key
}
Default {
$Key = $Key -replace 'HKEY_CLASSES_ROOT', 'HKCR:'
$Key = $Key -replace 'HKEY_CURRENT_CONFIG', 'HKCC:'
$Key = $Key -replace 'HKEY_USERS', 'HKU:'
$Key = $Key -replace 'HKEY_CURRENT_USER', 'HKCU:'
$Key = $Key -replace 'HKEY_LOCAL_MACHINE', 'HKLM:'
return $Key
}
}
}
Update: I found get-itemproperty2 code at here with a name Get-RegistryItemProperty, and I just modified it with my RegRootReplace for possible powershell path issues. The result is this:
<#
Get-ItemProperty2 -> https://stackoverflow.com/questions/42963661
Get-RegistryItemProperty -> https://carlbarrett.uk/use-powershell-to-search-for-and-delete-registry-values
#>
function Get-RegistryItemProperty {
param(
[Parameter(ValueFromPipeline)]
$Key
)
process {
$Key.GetValueNames() | ForEach-Object {
$Value = $_
[pscustomobject] @{
Path = $(RegRootReplace -Key $Key -ResultType 'registry')
Name = $Value
Value = $Key.GetValue($Value)
Type = $Key.GetValueKind($Value)
}
}
}
}
I also created a function to search registry for a search key.
function Search-Registry-KeyValueData {
<#
.EXAMPLE
Search-Registry-KeyValueData $SearchKey
#>
param(
[Parameter(ValueFromPipeline)]
[string[]]
$SearchKey
)
$SearchKey = $SearchKey.Replace(' ', '(([\s,\.])?)+')
$RegHivesStr = @('HKCR:', 'HKCC:', 'HKU:', 'HKCU:', 'HKLM:')
$RegHives = $($RegHivesStr | ForEach-Object { Get-ChildItem -Path $(RegRootReplace -Key $_ -ResultType 'registry') -Recurse -ErrorAction 'SilentlyContinue' })
$RegHives | ForEach-Object {
$_ | Get-RegistryItemProperty | Where-Object { $_.'Path' -match $SearchKey -or $_.'Name' -match $SearchKey -or $_.'Value' -match $SearchKey }
}
}
Update: However, I still have an issue when I run my Search-Registry-KeyValueData code:
Get-RegistryItemProperty : Exception calling "GetValueNames" with "0" argument(s): "Silmek üzere imlenmiş bir kayıt defteri anahtarına geçersiz işlem yapılmak istendi."
My system is in Turkish language. It says that "An illegal operation was attempted on a registry key marked for deletion.". I need help for this issue.
-ErrorAction SilentlyContinue? Values can be enumerated by callingGet-ItemPropertyon a key.-ErrorAction SilentlyContinue. I had given up on that because it had failed to suppress errors with an earlier command I tried, but it does work with this one, so I edited my question accordingly. However, I get an errorget-itemproperty : Specified cast is not validwhen I execute this command:get-childitem -path hkcu:\ -recurse -erroraction silentlycontinue | get-itemproperty | where {$_ -like "*foo"}. What would I need to do differently?where {$_.PSParentPath -like "*foo"}for example