10

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.

2 Answers 2

11

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}'
Sign up to request clarification or add additional context in comments.

Comments

4

I just cleaned it up a bit and this version gives me the right string.

function com_registeredpath()
{  
    param([string]$guid)
    New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT | Out-Null
    $key = Get-Item "HKCR:\CLSID\$guid\InprocServer32"
    $values = Get-ItemProperty $key.PSPath

    return $values.'(default)'
}

com_registeredpath "{0000002F-0000-0000-C000-000000000046}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.