Question

The Powershell script that performs certain activity is placed in the right-click context menu, and can be called from any placed on the file system.

Q: How to pass argument that contains a full path of place where user clicked on the context menu that has launched the script?

"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" 
-File "D:\Run-CoolScript.ps1" .......

Above is stated command in the registry to specify context menu action. Instead of the dots, what should I put so that script know from where it has been launched?

Many Thanks

Was it helpful?

Solution 2

Solution: Variable that contains current place from where the context menu has been called by clicking right-click on empty space i %V.

So registry Command entry became:

"C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe" 
-File "D:\Run-CoolScript.ps1" "%V"

N.B. Of course D:\Run-CoolScript.ps1 has "to know" how to handle the input parameters!

OTHER TIPS

I've used something like this in the past:

... -Command `"& {[Environment]::CurrentDirectory=(Set-Location -LiteralPath:'%L' -PassThru).ProviderPath; D:\Run-CoolScript.ps1}`"

Or if you aren't making any calls into .NET that relies on the current dir being set correctly you can simplify to this:

... -Command `"& {Set-Location -LiteralPath:'%L'; D:\Run-CoolScript.ps1}`"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top