This question already has an answer here:
这个问题在这里已有答案:
- What's the best way to determine the location of the current PowerShell script? 11 answers
- 确定当前PowerShell脚本位置的最佳方法是什么? 11个答案
I run a PowerShell script. How do I get the directory path of this script I run?
我运行PowerShell脚本。如何获取我运行的此脚本的目录路径?
How to do this?
这个怎么做?
1 个解决方案
#1
118
PowerShell 3 has the $PSScriptRoot
automatic variable:
PowerShell 3有$ PSScriptRoot自动变量:
Contains the directory from which a script is being run.
包含运行脚本的目录。
In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.
在Windows PowerShell 2.0中,此变量仅在脚本模块(.psm1)中有效。从Windows PowerShell 3.0开始,它在所有脚本中都有效。
Don't be fooled by the poor wording. PSScriptRoot
is the directory of the current file.
不要被糟糕的措辞所迷惑。 PSScriptRoot是当前文件的目录。
In PowerShell 2, you can calculate the value of $PSScriptRoot
yourself:
在PowerShell 2中,您可以自己计算$ PSScriptRoot的值:
# PowerShell v2
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
#1
118
PowerShell 3 has the $PSScriptRoot
automatic variable:
PowerShell 3有$ PSScriptRoot自动变量:
Contains the directory from which a script is being run.
包含运行脚本的目录。
In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.
在Windows PowerShell 2.0中,此变量仅在脚本模块(.psm1)中有效。从Windows PowerShell 3.0开始,它在所有脚本中都有效。
Don't be fooled by the poor wording. PSScriptRoot
is the directory of the current file.
不要被糟糕的措辞所迷惑。 PSScriptRoot是当前文件的目录。
In PowerShell 2, you can calculate the value of $PSScriptRoot
yourself:
在PowerShell 2中,您可以自己计算$ PSScriptRoot的值:
# PowerShell v2
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition