rpg
rpg

Reputation: 1652

Running script on selected folder/drive using right click on windows explorer

Environment: Windows 7

With the help of straight forward article from Scott, I am able to have "PowerShell Here" as the right click item of windows explorer.

Right clicking "PowerShell Here" opens a powershell command prompt with selected folder as the current working directory.

But I want little bit different which I am not able to do - inplace of opening the powershell prompt, I want to run a powershell script taking argument as the selected drive/folder/filename!

So, I updated the following line of "powershellhere.inf" file,

;existing one
;HKCR,Drive\Shell\PowerShellHere\command,,,"""C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe"" -NoExit ""cd '%1'"""

;updated one, added -Command <ScriptFile>
HKCR,Drive\Shell\PowerShellHere\command,,,"""C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe"" -Command d:\temp.ps1 -NoExit ""cd '%1'"""

But when I right click and select the "PowerShell Here", it's not running the script in the selected drive/folder/file, it's running in C:\Windows\System32 folder.

Upvotes: 0

Views: 3301

Answers (2)

Matt
Matt

Reputation: 46730

The string I believe you are looking for:

"""C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe"" -NoExit -File ""C:\temp\test.ps1"" ""'%1'""" 

-NoExit had to be before the -File else it was being picked up as an argument to the ps1 file. Also you need to put a full file path. I'm sure you could use environment variables. In my script i just had Write-Host $args[0] which output the path.

I was having an issue with the path being passed at first. I think the single quotes were not the ones I expected them to be inside the script. My script now changes directory successfully. Contents of test.ps1

$location = $args[0].Trim("'")
Write-Host "Path is valid = $(Test-Path $location)"
Set-Location $location 

Upvotes: 1

briantist
briantist

Reputation: 47862

Use -File instead of -Command and get rid of the cd stuff:

HKCR,Drive\Shell\PowerShellHere\command,,,"""C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe"" -File d:\temp.ps1 -NoExit

-Command is for literal commands on the command line. You may also want to set the -ExecutionPolicy if you have an issue with that.

If you don't want the prompt to stick around after executing the script, get rid of -NoExit.

Upvotes: 0

Related Questions