DriveLetter not working

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

when sharing any code please remember to format it as code.
You can use the ā€œPreformatted Textā€ button from the toolbar.
Guide to Posting Code

1 Like

thanks for editing everything.

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.

Start-Process powershell -Verb RunAs

-Verb RunAs makes it Run As Administrator. The %1 represents the path, right-clicked

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.

How do I extract the DriveLetter: part from the path of ā€˜%1’ ?

that i do not know. I’ve never used that %1 variable in any context.

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

%~d1

1 Like

%~d1
But this is for .BAT scripts. Its not accepted in .ps1 scripts

Your shortcut is using cmd to call powershell. Please read the answers you are provided.

But the .ps1 script is NOT run as administrator. The UAC prompt did not come up.

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.

I would be using something more reliable and faster than registry keys like. . .

Get-Disk | Where BusType -eq "USB"

Then to find the DriveLetter: part from the path you could be using the CMDLETs built in for doing that. (Get-Command -Noun Path)

Split-Path C:\UTILS\Scripts -Qualifier

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.