Powershell – Get Current Drive Letter Using Powershell

powershell

All I'm looking for is a way to get the current drive letter that I am currently running the powershell script from.

I know I can get the current path using Get-Location but is there a way to extract the drive letter without doing some kind of substring operation?

Best Answer

Yes, you can get the drive letter without string operations:

(get-location).Drive.Name

Remember, that PowerShell seldom returns strings, but rich objects. You can always examine your further options from a result by using get-member.

Also note, that if you are not on the file system provider, the Name might not be a one-character string.

Edit:

As per x0n's comment below, here is a shorthand version:

$pwd.drive.name