Search code examples
powershellregistrycontextmenu

How to change the value of a parameter in the registry with the path of the selected document?


I want to create a context menu function to copy the path of the clicked document to the registry.

I tried to create a working script, but I couldn't. This is my first powershell script. I searched for information on the internet and I couldn't find a way to add a path to the document in the registry. Running this script does not change the value of the Path parameter.

If I replace %L with a string or integer in the script, it will work normally. But I don't know how to get the path of the document so I can save the path in the registry.

How to change the value of a parameter in the registry with the path of the selected document? Please help me with a piece of advice or a solution to my question.

enter image description here

The location of the section where the path is to be saved:

HKEY_CURRENT_USER\SOFTWARE\Test

Section parameters:

enter image description here

The powershell script I used:

PowerShell -command {Set-ItemProperty -Path "HKCU:\SOFTWARE\Test" -Name "Path" -Value %L}

Script location in the registry :

HKEY_CLASSES_ROOT\*\shell\Copy file path\command

Solution

  • I found the solution. I had to replace the parentheses {} with " and put the path between the two ' and instead of %L I had to put %1. I don't know why without these fixes the script wouldn't work. In the command, use %1 as a placeholder for the path to the file that was right-clicked.

    Example: PowerShell -command "Set-ItemProperty -Path HKCU:\SOFTWARE\Test -Name Path -Value 'C:\Program Files\Windows Defender\shellext.dll'"

    Final script: PowerShell -command "Set-ItemProperty -Path HKCU:\SOFTWARE\Test -Name Path -Value '%1'"