PowerShell跨平台中的点斜杠和反斜杠
我正在编写PowerShell脚本,该脚本使用PowerShell跨平台中的点斜杠和反斜杠,powershell,Powershell,我正在编写PowerShell脚本,该脚本使用&.\other\script.ps1调用其他PowerShell脚本,但我认为反斜杠\是Windows的东西,我希望我的脚本能够跨平台工作 我注意到Windows中的PowerShell确实接受&./other/script.ps1,但这可能是偶然的。PowerShell是否总是将我的反斜杠翻译到主机平台 在PowerShell中跨平台处理路径分隔符的最佳方法是什么?根据我的经验,Windows PowerShell很乐意接受\或/作为路径分隔符,
&.\other\script.ps1
调用其他PowerShell脚本,但我认为反斜杠\
是Windows的东西,我希望我的脚本能够跨平台工作
我注意到Windows中的PowerShell确实接受&./other/script.ps1
,但这可能是偶然的。PowerShell是否总是将我的反斜杠翻译到主机平台
在PowerShell中跨平台处理路径分隔符的最佳方法是什么?根据我的经验,Windows PowerShell很乐意接受
\
或/
作为路径分隔符,因此跨平台的一种方法是始终使用/
或者,您可以使用Path
cmdlet为您处理建筑路径,例如:
Join-Path -Path $Pwd -ChildPath (Join-Path -Path 'Other' -ChildPath 'script.ps1')
或获取当前目录中文件的路径:
Resolve-Path test.txt
路径cmdlet:
~> get-command *-path* | Select Name
Name
----
Convert-Path
Join-Path
Resolve-Path
Split-Path
Test-Path
我没有足够的信用给@mark wragg接受的答案添加评论,但我只想指出,我开始只在跨平台脚本等中使用前斜杠,并且遇到了符号链接问题 例如,运行
newitem-itemtype-symbolicink-path TestScript8.ps1-target./TestScript.ps1
您将无法在Windows上的VS代码中打开TestScript8.ps1