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?
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
fromWin32_PnpEntity
and Device Manager: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.
You can get the current file, and use this to get the current usb drive.
If Powershell is more powerful than regular windows CMD.exe then why do I only have to use the command
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.
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:
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.
I added a VolumeLabel("MyToolBox") to my usb Stick and put following line in the profile.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...