I have a function in Powershell that returns the path from where a COM dll is registered; within the function correct path is returned but when this function is invoked, there is an extra string "HKCR" prefixed to the output
function com_registeredpath()
{
param([string]$guid)
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
$key = Get-Item "HKCR:\CLSID\$guid\InprocServer32"
$values = Get-ItemProperty $key.PSPath
[string] $defaultValue = [string] $values."(default)"
write-host ">>>: $defaultValue" # returns a value like: c:\somefolder\somefile.dll
remove-psdrive -name HKCR
return $defaultValue
}
write-host "~~~" (com_registeredpath "{00F97463-DF44-11D1-BED5-00600831F894}") # returns a value like: HKCR c:\somefolder\somefile.dll
Can some one explain this strange behaviour? I would expect both return values to be same.
One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .
For example, this command displays the items directly within PowerShell drive HKCU: , which corresponds to the HKEY_CURRENT_USER registry hive: PowerShell Copy. Get-ChildItem -Path HKCU:\ | Select-Object Name.
You can use Get-ChildItem to view registry keys and Set-Location to navigate to a key path. Registry values are attributes of a registry key. In the Registry drive, they are called Item Properties. A registry key can have both children keys and item properties.
The Test-Path cmdlet determines whether all elements of the path exist. It returns $True if all elements exist and $False if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element.
I don't get a path prefixed with the reg hive. First, you need to suppress the result of the new psdrive, you don't want the function to return anything but the dll path (I assigned it to null). Lastly, you can get the value without crating a psdrive, just use the provider path for HKCR
function Get-ComRegisteredPath
{
param( [string]$Guid )
try
{
$reg = Get-ItemProperty "Registry::HKEY_CLASSES_ROOT\CLSID\$Guid\InprocServer32" -ErrorAction Stop
$reg.'(default)'
}
catch
{
Write-Error $_
}
}
PS> Get-ComRegisteredPath -Guid '{00F97463-DF44-11D1-BED5-00600831F894}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With