windows - 如何在 PowerShell 中将位置与主文件夹进行比较

标签 windows powershell shell

我曾尝试在特定位置运行脚本,但我无法运行,我意识到这与以下代码有关,我认为这些代码应该返回 True,但如果您位于 Windows 10 的主文件夹中,它实际上会返回 False。

$loc=Get-Location 
$loc -eq $HOME  =>> Should return True but it is not.

因此,我们将不胜感激任何评论或回答。

最佳答案

Get-Location 返回一个位置对象,而$home 只是一个字符串。将 $loc 转换为字符串,或将 $home 与其 Path 属性进行比较:

$loc.Path -eq $home
# or 
"$loc" -eq $home

由于 PowerShell 的 -eq 运算符根据左侧操作数的类型运行,您也可以翻转两个操作数,它也能正常工作:

$home -eq $loc

关于windows - 如何在 PowerShell 中将位置与主文件夹进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62410254/

相关文章:

在 awk 中调用将记录作为参数的 c 代码

linux - 使用 shell 计算学生的平均分

c# - Windows Azure 持久存储技巧

arrays - 是否可以在 PowerShell 中使 IndexOf 不区分大小写?

Powershell 测试路径然后删除项目但文件正在被另一个进程使用

regex - 在 Powershell 中使用函数替换

android - 从脚本chmod android文件系统

c++ - Visual Studio 2015 : switch between remote compilation (on Raspberry) and local compilation (on Windows)

windows - winddk : __iob_func redefinition

windows - 在 Windows 上运行 grunt.cmd 的多个问题