1

I'm actually writing a node script that detect if a specific USB is plugged, then it copy it content to Desktop. This is for Windows principally. To do this I manually check if 'E:\' path exists , 'F:\' , etc...

But I need to be sure that devices are the ones that I need. They got specific names, for example:

MTR12345 or MTR23RR5 or MTRTGX23.

And I need to know theses names. I searched for differents nodejs and powershell solutions but no ones fit my needs.

I need to get the name of the device that is mounted at E:\ . I'm totally new to PowerShell and NodeJS as well.

How can I do this ? Thanks for your help.

Sami Boudoukha
  • 539
  • 10
  • 25
  • `Get-WmiObject Win32_logicaldisk` would get that I would think. You would need to filter on the VolumeName from the sounds of it. `Get-WmiObject Win32_logicaldisk | Where-Object{"MTR12345", "MTR23RR5", "MTRTGX23" -contains $_.VolumeName} | Select -Expand DeviceID`? Is there something you tried or something you got stuck on? – Matt Nov 24 '15 at 18:09
  • I tryed many usb frameworks that dont fit my needs, and powershell commands too. Thanks for you answer, your commands tell me where the MTR are plugged, even if i needed the opposit (I know the Mountpoint, but need the name of the Device. For using your command, I need to know the exact name of my MTR, but it change everytime). I tryed your command with RegExp but it don't seems to work. I tryed: `Get-WmiObject Win32_logicaldisk | Where-Object{"MTR[A-Za-z0-9]+" -contains $_.VolumeName} | Select -Expand DeviceID` – Sami Boudoukha Nov 25 '15 at 12:28
  • 1
    But is the volumename what you are actually looking for? If you needed to use regex then your clause would be this. `Where-Object{$_.VolumeName -match "MTR[A-Za-z0-9]+"}`. You never explicitly said that you needed partial or regex matched. – Matt Nov 25 '15 at 12:32
  • Your solution worked like a charm Matt. Answer my question then i can accept you as a Answer. – Sami Boudoukha Nov 25 '15 at 18:38

1 Answers1

2

Sounds like you are just looking for the volume names. The WMI class Win32_logicaldisk would return that for mounted devices. Assuming it was populated of course. In it's simplest form:

Get-WmiObject Win32_logicaldisk | Where-Object{$_.VolumeName -eq "MyUSBKey"}

You have some specific examples and a regex query that you are trying to use to narrow down the results. So if you want to match a regex query:

Get-WmiObject Win32_logicaldisk | 
    Where-Object{Where-Object{$_.VolumeName -match "MTR[A-Za-z0-9]+"}} |          
    Select -Expand DeviceID

You could even simplify that if you wanted. Just match volumes that start with "MTR". Not as perfect as the other one but just as simple.

Get-WmiObject Win32_logicaldisk | 
    Where-Object{Where-Object{$_.VolumeName -match "^MTR"}} |          
    Select -Expand DeviceID
Matt
  • 45,022
  • 8
  • 78
  • 119