Powershell中的$ExecutionContext.SessionState.Path.CurrentLocation和$pwd有什么区别?

Powershell中的$ExecutionContext.SessionState.Path.CurrentLocation和$pwd有什么区别?,shell,powershell,prompt,Shell,Powershell,Prompt,当我检查Powershell的提示功能时,我注意到,$ExecutionContext.SessionState.Path.CurrentLocation用于获取当前路径,因此它与$pwd之间有什么区别?$ExecutionContext.SessionState.Path.CurrentLocation和$pwd实际上有区别 当您使用$PWD时,实际上您将得到$ExecutionContext.SessionState.Path.CurrentLocation的结果 然而,如果您使用$Exec

当我检查Powershell的提示功能时,我注意到,
$ExecutionContext.SessionState.Path.CurrentLocation
用于获取当前路径,因此它与
$pwd
之间有什么区别?

$ExecutionContext.SessionState.Path.CurrentLocation
$pwd
实际上有区别

当您使用
$PWD
时,实际上您将得到
$ExecutionContext.SessionState.Path.CurrentLocation
的结果

然而,如果您使用
$ExecutionContext
,您将获得更多关于执行上下文的属性

PS C:\>$ExecutionContext.SessionState.Path.CurrentLocation 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 路径 ---- C:\ PS C:\>$ExecutionContext.SessionState.Path.CurrentLocation.GetType().FullName System.Management.Automation.PathInfo 私人秘书长:\>$PWD 路径 ---- C:\ PS C:\>$PWD.GetType().FullName System.Management.Automation.PathInfo
因此,基本上区别在于,
$ExecutionContext.SessionState.Path.CurrentLocation
$PWD

需要更多的键入。确实,
$PWD
$ExecutionContext
获取其值

这两个变量之间的关键区别是可以覆盖
$pwd
,但
$ExecutionContext
是常量(只读)

$ExecutionContext
旨在模拟cmdlet作者可用的接口
$pwd
只是获取当前路径的一种方便方法


因此,如果您需要获取路径而不担心任何人会弄乱
$pwd

的值,建议使用
$ExecutionContext
,这真的是答案吗?当然,
ExecutionModel
本身比
SessionState.Path.CurrentLocation
@Christian.K]拥有更多的信息。我的意思是,它就像一棵树,包含许多分支内容,其中一个分支是$PWD。但不是主要分支它位于次要分支中,如sessionstate.pathYes,但OP会询问到“分支”之间的区别:$ExecutionContext.sessionstate.Path.CurrentLocation和$PWD。