Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Drive Letter Using Powershell

Tags:

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?

like image 532
Ross Hambrick Avatar asked Aug 25 '11 18:08

Ross Hambrick


People also ask

How do I get the current drive in PowerShell?

The Get-Location cmdlet returns the current directory of the current PowerShell runspace. This cmdlet is designed to work with the data exposed by any provider. To list the providers in your session, type Get-PSProvider .

How do I view drives in PowerShell?

For a list of the Windows PowerShell drives in your Windows PowerShell session, use the Get-PSDrive cmdlet. Although the drives in the display vary with the drives on your system, the listing will look similar to the output of the Get-PSDrive command shown above.

How do I list a network drive in PowerShell?

We can query network drives using PowerShell with the Get-WmiObject cmdlet. You can query the Win32_MappedLogicalDisk WMI class which represents network storage devices that are mapped as logical disks on the computer system. Note: You can query a remote computer with the $computer variable.

What is PSDrive in PowerShell?

The New-PSDrive cmdlet creates temporary and persistent drives that are mapped to or associated with a location in a data store, such as a network drive, a directory on the local computer, or a registry key, and persistent Windows mapped network drives that are associated with a file system location on a remote ...


1 Answers

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
like image 132
driis Avatar answered Sep 16 '22 16:09

driis