How to find my USB flash drive's path with PowerShell

心已入冬 提交于 2019-12-23 04:38:13

问题


I am preparing a ps1 file. It will install some programmes silently. But my programmes' setup files were saved in my USB flash drive. In my ps1 file,

cd E:\User\User_Setups

This path is my USB flash drive's path. But it will change on the other machine. Maybe G:\, F:\ etc. Naturally, I don't want to change this path for every different machines. How PowerShell find my USB flash drive's path by a command-line?


回答1:


I added a VolumeLabel("MyToolBox") to my usb Stick and put following line in the profile.ps1:

Get-DriveInfo | % { if( $_.VolumeLabel -eq "MyToolBox"){ Set-Location $_.Name; ./Startup.ps1}}

Get-DriveInfo comes from the module Pscx: http://pscx.codeplex.com/ You need to import this in you profile too...

The Startup.ps1 script is in the root of my usb stick and registers aliases on the stick for use in the session...




回答2:


I've done this with WMI: using the device type to get to the drive letter. in simplified form (real script has logging and error handling). I initially obtained $deviceCaption from Win32_PnpEntity and Device Manager:

    $objs = @(Get-WmiObject -Query "select Caption,__RELPATH from Win32_PnpEntity where caption=""$deviceCaption""")
    if ($objs.Length -eq 0) {
        throw "MP3 Player is not connected"
    } elseif ($objs.Length -gt 1) {
        throw "Seem to be multiple MP3 players connected"
    }
    $relPath = $objs[0];

    $objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_DiskDrive")
    $relPath = $objs[0].__RelPath;

    $objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_DiskPartition")
    $relPath = $objs[0].__RelPath;
    $objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_LogicalDisk")
    $relPath = $objs[0].__RelPath;
    Write-Debug "RelPath #4: $($objs[0].__RelPath), drive: $($objs[0].DeviceID)"

    $objs[0].DeviceID

That final expression returns the drive name, something like: Q: (it does include to colon).

Note this assumes the device has a single disk with a single partition.




回答3:


There are probably several good ways to do it. Personally, I'd put an id file disk.id on drive and just search each drive programmatically until I found the id file with the id I'm looking for. Something like this:

#Start i at 65 to represent A
i=65
do {
  $idFile = Get-Content [char]$i:\disk.id -totalcount 1
  if( $idFile -eq "MyIdDrive" ) { #whatever your first line in the id file may be
       Write-Host "[char]$i is my drive!"
  }
  $i++
}
while ($i -le 65+26)

It's a brute force method and you may need to error handle the Get-Content but it should work on most Windows installs. The only case where you'd run into problems is with double case drive names and then you'd just need to create a more sophisticated loop.




回答4:


If Powershell is more powerful than regular windows CMD.exe then why do I only have to use the command

ECHO %~dp0

in CMD.exe to answer your question? Seems to me you have to write a lot of extra code to get the relative path of the batch or cmd file information, and this comes up often in batch scripts. Powershell fail.

http://ss64.com/nt/syntax-args.html for more info.




回答5:


You can get the current file, and use this to get the current usb drive.

$currentDirectory = $myInvocation.MyCommand.ScriptBlock.File | Split-Path | Get-Item
$currentDirectory.PSDrive.Root


来源:https://stackoverflow.com/questions/9704328/how-to-find-my-usb-flash-drives-path-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!