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

5

0

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?

Dearr answered 14/3, 2012 at 14:53 Comment(0)
B
3

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...

Bibliophile answered 14/3, 2012 at 14:58 Comment(3)
MyToolBox is the volume label for the flash drive. You can label it whatever you want and then adjust the script to look for that new label instead of MyToolBox.Laminar
The term 'Get-DriveInfo' is not recognized as the name of a cmdlet, function, script fil e, or operable program. Check the spelling of the name, or if a path was included, verif y that the path is correct and try again. At G:\startup.ps1:1 char:14 + Get-DriveInfo <<<< | % { if( $_.VolumeLabel -eq "MyToolBox"){ Set-Location $_.Name; . /startup.ps1}} + CategoryInfo : ObjectNotFound: (Get-DriveInfo:String) [], CommandNotFoun dException + FullyQualifiedErrorId : CommandNotFoundException How can i fix this error?Dearr
I'm sorry, I forgot I have a module for that: the powershell community extentions ( or PSCX) they have a lot of usefull commands, I'll add a link in my answer...Bibliophile
W
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.

Wiley answered 14/3, 2012 at 15:10 Comment(0)
U
0

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.

Uriah answered 14/3, 2012 at 15:11 Comment(0)
D
0

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.

Diastasis answered 23/9, 2013 at 21:22 Comment(2)
It seems that you measured the "power" of a script language based upon how short the commands are. Five years later, I'm curious if you still think the same way.Discordant
Powershell is definitely powerful and appreciate it now, but I definitely still do prefer simplicity. I do have to admit reading my answer over I do seem rather put off by Powershell. ahh to be young and naive. I did find this answer though: $ScriptDir = Split-Path $script:MyInvocation.MyCommand.PathDiastasis
N
0

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
Nitride answered 23/9, 2013 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.