1
votes

I am trying to figure out a Powershell command that would allow me to capture the drive letter of the only USB drive plugged into the computer, and then be able to recall that variable like this:

dir %usbdrive%

I have used this command to show the stats of the only USB drive:

Get-WmiObject Win32_Volume -Filter "DriveType='2'"

But how can I either store the drive letter in a variable or just change the drive letter to a completely different letter like "T"?

3

3 Answers

1
votes

Storing the drive letter is simple, you pipe your result to Select -ExpandProperty DriveLetter, and as is pretty basic in PowerShell you assign the result of that command to a variable like:

$USBDrive = Get-WmiObject Win32_Volume -Filter "DriveType='2'"|select -expand driveletter

Now, that does include a trailing colon on it, so you might want to trim that off like:

$USBDrive = $USBDrive.Trim(":")

That would leave you with only the letter of the drive. Changing the drive letter of a known drive is another matter. If you really want to get into that let us know, or possibly better yet post a new question asking how to change a drive letter.

1
votes

Easiest way to get to the drive letter, is to drill down to the "DriveLetter" property. This is a [String] so you can use the Substring method to extract just the first character like this:

$USBDrive = (Get-WmiObject Win32_Volume -Filter "DriveType='2'").DriveLetter.Substring(0,1)

Note: This will only work if you have exactly one USB drive. In real life you should always check:

$USBDrives = Get-WmiObject Win32_Volume -Filter "DriveType='2'"
if ($USBDrives  -is [system.array]){
    $USBDrive = $USBDrives[0].DriveLetter.Substring(0,1)
}else{
    $USBDrive = $USBDrives.DriveLetter.Substring(0,1)
}

To answer the second part of your question, you can change the drive letter (and other properties) of your USB drive using Set-WmiInstance command.

$USBDrives = Get-WmiObject Win32_Volume -Filter "DriveType='2'"
if ($USBDrives  -is [system.array]){
    $USBDriveLetter = $USBDrives[0].DriveLetter
}else{
    $USBDriveLetter = $USBDrives.DriveLetter
}

$USBDrive = Get-WmiObject win32_volume -Filter "DriveLetter = '$USBDriveLetter'"
Set-WmiInstance -InputObject $USBDrive -Arguments @{DriveLetter = "F:";Label = "Test"}
0
votes

I feel like you didn't really try to hard in figuring this out. First did you look at the output of the first command? Look at the truncated output from my own machine.

....
DirtyBitSet                  : True
DriveLetter                  : H:
DriveType                    : 2
....

Knowing that the answer to what you are looking for would be to extract the property DriveLetter. The following would return "H:". I use Select-Object -First 1 to guarantee that only one object is returned.

Get-WmiObject Win32_Volume -Filter "DriveType='2'" | Select-Object -First 1 -ExpandProperty Driveletter

As for the second part a good TechNet blog already has that answer:

$drive = Get-WmiObject Win32_Volume -Filter "DriveType='2'" | Select-Object -First 1
Set-WmiInstance -input $drive -Arguments @{DriveLetter="Q:"; Label="New Label"}