2

I have written a GUI app in PowerShell. It has one parameter.

At the beginning of the script I have:

param (
    [string]$Path
)

Before the Form appears, I fill in a control with $Path:

$Form.Add_Load({
    $txtSourceFile.Text = $Path
})

When I run the script interactively in PowerShell it seems to be working as expected. The txtSourceFile control is filled in with the argument from the command line.

.\ghead.ps1 -Path C:\src\data\ghead.ps1

However, when I run this as a File Explorer extension, it does not appear to receive the argument from the command line. The registry setting for the command is:

C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.EXE C:\\Users\\lit\\bin\\ghead.ps1 -Path %1

There is no error message. It simply does not fill in the $txtSourceFile.Text field. Am I missing something obvious? How can I get this to work?

The question suggested as a duplicate does not deal with running as a Windows File Explorer extension. I am already using the param() technique.

lit
  • 14,456
  • 10
  • 65
  • 119
  • Possible duplicate of [Pass parameter from batch file to the powershell script](http://stackoverflow.com/questions/6359618/pass-parameter-from-batch-file-to-the-powershell-script) – Clijsters Mar 26 '17 at 07:49
  • Don't you have to wrap this in a function and call the function to pass the parameter that way? I may be wrong but that is how I would do it. – nkasco Mar 26 '17 at 13:55
  • @nkasco - What would you suggest as the registry entry to run a function inside the .ps1 file? – lit Mar 26 '17 at 20:51
  • What will be the value of path here, can you hard code the path in registry and try ? – Prasoon Karunan V Mar 28 '17 at 03:04
  • Can you please share how can I make an explorer shell extension in powershell? Particularly how to create registry entries which regsvr32.exe does for native DLLs? – Sahil Singh Mar 22 '22 at 05:16

1 Answers1

1

The core of the problem was an extraneous character in the Users directory name which was not presented in the question. The OP probably needs new glasses glued to his face so that he can see better and not go without them.

C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.EXE -Command C:\Users\pwatson\bin\ghead.ps1 -Path

The final registry entry turned out to be:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE -WindowStyle Hidden -Command \\ASERVER\D$\Install\Utilities\ghead.ps1 -Path %1
lit
  • 14,456
  • 10
  • 65
  • 119