JohnM
JohnM

Reputation: 31

Passing a filename into associated Powershell script

I'm creating a script to provide choices of programs to launch when the user double-clicks upon a file in Windows Explorer. I associate the selected file extension with the script that has been wrapped into an .exe.

But inside the Launcher script, I need to use the filename the user double-clicked in order to create a command line string to launch the selected program.

How can I get that filename inside the script? e.g. "%1"

Note: a Windows shortcut menu is not appropriate as I will also use this script in a web page.

Thanks!

Upvotes: 2

Views: 10250

Answers (2)

JohnM
JohnM

Reputation: 31

The answer is a combination of @sodawillow and @user2460798 answers. I'm grateful that you each gave me your input. Thankyou!

  1. The script must define a parameter to receive the path & filename from the double-click (note that Param declarations must be prior to any other code) "Param([String]$FileNameSelected)".
  2. The script must use the param $FileNameSelected as an argument when launching a program.
  3. Associate your filename extension with your script (wrapped to an .exe - I used PS2EXE).

Here's a sample script:

Param([String]$FileNameSelected)

$title = "Launch Menu"
$message = "Do you want to launch program A, B, or C?"
$pA = New-Object System.Management.Automation.Host.ChoiceDescription "&Notepad", "Launches Notepad."
$pB = New-Object System.Management.Automation.Host.ChoiceDescription "&B", "Launches program B."
$pC = New-Object System.Management.Automation.Host.ChoiceDescription "&C", "Launches program C."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($pA, $pB, $pC)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
    {
        0 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'notepad.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "c:\windows\system32"
          }
        1 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'ProgramB.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "C:\Program Files (x86)\Test"
          }
        2 {
           $wshell = New-Object -ComObject Wscript.Shell
           $executable = 'ProgramC.exe'
           $argument = '"' + $FileNameSelected + '"'                                
           Start-Process $executable $argument -workingdirectory "C:\Program Files\Test"
          }
    }

Upvotes: 1

sodawillow
sodawillow

Reputation: 13176

The $args variable holds the arguments passed to a PowerShell script.

If you put, for example :

Write-Host $args[0]

in your script, you will see the first argument passed in the script call.

PS > .\myscript.ps1 "test string"

would output

test string

Upvotes: 1

Related Questions