Simen K
Simen K

Reputation: 49

How to find a usb drive letter trhough the Run box in windows and use it to run a script

I am trying to run a script from the run box in windows. The problem is that this is on a usb and I want to be able to do this on different computers. The usb is called "bashbunny", but the drive letter will change depending on the computer. How do i find the drive letter and launch the script that is on the usb through the Run box?

Sorry if im not able to explain better :)

What I have done so far:

powershell ".((gwmi win32_volume -f 'label=''BashBunny''').Name+'payloads\switch1\r.cmd')"

Upvotes: 0

Views: 334

Answers (2)

aschipfl
aschipfl

Reputation: 34909

Here is a pure solution (as you also tagged your question accordingly):

(for /F "skip=1" %I in ('wmic Volume where ^(DriveType^=2 AND Label LIKE "BashBunny"^) get DriveLetter') do @for /F %J in ("%I") do @set "DRIVE=%J") && call "^%DRIVE^%\payloads\switch1\r.cmd"

Or, with an alternative wmic command line:

(for /F "skip=1" %I in ('wmic LogicalDisk where ^(DriveType^=2 AND VolumeName^="BashBunny"^) get DeviceID') do @for /F %J in ("%I") do @set "DRIVE=%J") && call "^%DRIVE^%\payloads\switch1\r.cmd"

Upvotes: 0

Simen K
Simen K

Reputation: 49

After tinkering a bit I found the solution:

powershell -executionpolicy Bypass ".((gwmi win32_volume -f 'label=''BashBunny''').Name+'payloads\switch1\r.ps1')"

Upvotes: 1

Related Questions