How do we get a drive by its label in PowerShell 5.0?


I have an autorun.inf file with the following contents on my external hard drive:

This labels my external hard drive once it’s plugged in as, MasterSword. I want to store a few scripts on it and include them in my $profile so they are loaded when PowerShell starts or when the profile is re-included (. $profile) at the interpreter.

As many know, using a hard-coded drive letter for external drives can lead to changing the reference to those external hard drive scripts every time the drive letter changes, resulting in the inclusion failing.

So I guess I have two questions:

  1. How do I get a hold of the drive label that was set in the autorun.inf?
  2. How do I translate that drive label into the drive letter so I can reference the scripts stored on it?


I did a little more research and came up with this little snippet:

To answer #1:

To answer #2:

And together, they would be:

Or for another interpretation:

Where the necessary drive letter is stored in $scriptDriveLetter.


How do we get a drive by its label in PowerShell 5.0? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply