简体   繁体   English

使用Powershell获取当前驱动器号

[英]Get Current Drive Letter Using Powershell

All I'm looking for is a way to get the current drive letter that I am currently running the powershell script from. 我所寻找的只是一种获取当前正在运行powershell脚本的当前驱动器号的方法。

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? 我知道我可以使用Get-Location当前路径,但是有没有一种方法可以在不执行某种子字符串操作的情况下提取驱动器号?

Yes, you can get the drive letter without string operations: 是的,您无需使用字符串即可获取驱动器号:

(get-location).Drive.Name

Remember, that PowerShell seldom returns strings, but rich objects. 请记住,PowerShell很少返回字符串,而是富对象。 You can always examine your further options from a result by using get-member . 您始终可以使用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: 根据下面x0n的评论,这是一个简写形式:

$pwd.drive.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM