How Do You Detect the Hard Drive Letter That Your PowerShell Script (.ps1 File) Is Saved On?

Problem scenario
You have a PowerShell script that you want to be able to detect the hard drive letter it resides on.  How can PowerShell detect the hard drive letter that the .ps1 file itself is stored on?

Use this command: (get-location)

To get the drive letter to be in a variable named $dl, use this:
$dl = (get-location)

Now you can use $dl as the drive letter in the PowerShell script.   Creating composite strings such as c:\temp\various\ is easy with PowerShell.  Here is an example:

$lastsection = ':\temp\various\'
$compositeVar = $dl + $lastsection

# now compositeVar is the path x:\temp\various where x is the drive letter that the PowerShell script is ran from.

Leave a comment

Your email address will not be published. Required fields are marked *