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?
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 .
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.
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.
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 ...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With