当我检查Powershell的提示功能时,我注意到$ExecutionContext.SessionState.Path.CurrentLocation
用于获取当前路径,那么这与$pwd
之间的差异是什么?
答案 0 :(得分:1)
PS C:\> $ExecutionContext.SessionState.Path.CurrentLocation Path ---- C:\ PS C:\> $ExecutionContext.SessionState.Path.CurrentLocation.GetType().FullName System.Management.Automation.PathInfo PS C:\> $PWD Path ---- C:\ PS C:\> $PWD.GetType().FullName System.Management.Automation.PathInfo
因此,差异在于$ExecutionContext.SessionState.Path.CurrentLocation
需要比$PWD
更多的输入。
答案 1 :(得分:0)
$ExecutionContext.SessionState.Path.CurrentLocation
和$PWD
实际上有区别。
当您使用$PWD
时,您将获得$ExecutionContext.SessionState.Path.CurrentLocation
,
如果您使用$ExecutionContext
,您将获得有关执行上下文的更多属性。
答案 2 :(得分:0)
$pwd
确实从$ExecutionContext
获取其值。
这两个变量之间的关键区别是$pwd
可以被覆盖,但$ExecutionContext
是常量(Readonly)。
$ExecutionContext
旨在模仿cmdlet作者可用的界面。 $pwd
只是获取当前路径的便捷方式。
因此,如果您需要获取路径而不担心任何人可能会弄乱$ExecutionContext
的值,建议您使用$pwd
。