I am trying input this command line into context menu so when I right-click on the USB drive, that drive letter would pass into the .ps1 script and run commands using that driveletter. This script requires -Verb RunAS, but my version does not work.
The registry is
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Drive\shell\PassOnUSBLetter]
@="PassOnUSBLetter"
"HasLUAShield"=""
"Icon"="powershell.exe"
[HKEY_CLASSES_ROOT\Drive\shell\PassOnUSBLetter\command]
@="powershell.exe -ExecutionPolicy Bypass -File \"C:\\Users\\Tim\\USB.ps1\" %1 -verb runas"
.ps1 script is just
$driveLetter = $args[0]
Write-Host Drive Letter used: $driveLetter
# do more later on
I havenāt done this before so Iām not sure what the constraints are for adding commands, but what is the %1 variable represent? Iām also not sure about the -verb runas part as thatās not a Powershell parameter or a CMD parameter that iām familiar with.
ok, I see now that Start-Process has the -Verb RunAs parameter, but your original code isnāt using Start-Process.
From what I can see the value you put in the āCommandā key is going to be processed as a command, not PowerShell. If you need RunAs you might have to try the -Command parameter of powershell.exe and see if you can do a Start-Process in there with the -Verb parameter and point to your script.
First thing I recommend is to understand what is actually being used. If you see %1, thatās indicative that cmd is being used. (cmd uses % in variable names) In cmd, you can read all the help topics just like you can in powershell, for this specific question, I would read through the help for the for command by typing for /? at the cmd prompt. One specific section details how you can use substitution variable references.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
It is referring to named (letter) variables, but the argument variable work the same %1 is the first argument, %2 the second, and so on. %* is all arguments. In powershell, these are equivalent to $args[0] for the first argument, $args[1] for the second, and $args for all arguments. So for your specific question, how to extract the drive letter from %1 would be
Well thatās a different issue altogether. I was answering your question on a way to extract the drive letter. You donāt appear to have tested it, commented how itās for powershell (despite the explanation of what is calling your powershell.exe from the context menu in the first place), and then ignore this and move onto another issue. Iām sorry but youāre making it quite difficult to assist you.