如何找到位置/当前脚本在PowerShell中
问题描述:
是位于C路线我有一个脚本(script1.ps1):\ FolderA如何找到位置/当前脚本在PowerShell中
我可以在脚本插入什么PowerShell代码,这将打印什么都脚本的当前位置/路径。 (我们的目标是让这个脚本便携式)
即:
在folderA - 当,代码将打印C:\ FolderA(完整路径)
- 当Script1.ps1移到C:\ FolderB \ FolderC并运行,它打印C:\ FolderB \ FolderC的完整路径
任何帮助将不胜感激。
感谢
感谢@Nkosi和@Kori吉尔
我一直在寻找的答案是$ PSScriptRoot
有一点要注意的是,我发现我不得不定义在脚本运行脚本之前首先在脚本内部创建一个变量:
$Path = $PSScriptRoot
$Path
wi这工作,否则它没有
答
请参阅“get-help about_Automatic_Variables”。
阅读部分上:
- $ MyInvocation
- $ PSScriptRoot
- $ PSCommandPath
的可能的复制[?什么是确定当前的PowerShell脚本的位置最好的办法] (http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script) – Nkosi