myotis
myotis

Reputation: 423

Power Shell script on USB stick : how to determine drive letter for usb?

I want to use a USB stick for storing power shell scripts, scripts I will use on different PCs. Some of these scripts contain paths to the USB stick itself. For instance, if the USB stick is always assigned the drive letter K: my script file could contain the following line:

start notepad K:/my/path/mytext.txt

for making Power Shell open the file mytext.txt on the USB stick, by means of notepad.

The problem is that the drive letter assigned for the USB stick changes from PC to PC. How can I use a power script command to find the drive letter assigned to my USB stick?

I imagine this: by the use of <some command> I can store the drive letter assigned for my USB stick into a variable $myvar

$myvar = <some command>

and then use $myvar instead of the drive letter in the path I specify. If so, the above script line for opening my text file would look something like this:

start notepad $myvar/my/path/mytext.txt

Is this possible, and whats the name and the call to such a command?

Thanks in advance for all helpful suggestions.

Upvotes: 1

Views: 1924

Answers (1)

John Donnelly
John Donnelly

Reputation: 917

(Get-Location).Drive.Name will give your working directory's drive letter.

However the working directory may not be the script's location.

If you want the script to locate your USB drive and you know your flash drive's label, you can do something like this:

(Get-WmiObject Win32_Volume -Filter "DriveType='2'" |
    ?{$_.Label -eq 'SANDISK 16'}).DriveLetter

Replacing "SANDISK 16" with your drives label.

Upvotes: 1

Related Questions