Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning value from HKCR using Powershell

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.

like image 407
arung Avatar asked Dec 26 '11 05:12

arung


People also ask

How to Get registry value using PowerShell?

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 .

What is Hkcu in PowerShell?

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.

How do I read registry keys?

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.

How do I check the path in PowerShell?

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.


1 Answers

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}'
like image 188
Shay Levy Avatar answered Oct 05 '22 02:10

Shay Levy