测试路径始终返回False

时间:2019-04-16 19:12:07

标签: powershell windows-10

OS:Windows 10, Powershell:5.1

我有以下Powershell代码

Test-Path -Path "$drive`\"

其中$ drive是一个驱动器号,我正在检查是否将驱动器号映射到一个文件夹(无论是网络文件夹还是本地文件夹),并且我总是得到False值

1 个答案:

答案 0 :(得分:2)

您不能忽略路径中的冒号(:

Test-Path -Path C:\

此外,这里完全不需要转义(`)。

如果需要使用变量,则可以在字符串中使用变量定界符(${}):

Test-Path -Path ${drive}:\

但是,如果您的目标是检查驱动器是否已映射,则应使用:

Get-PSDrive -Name <driveletter>