Whats the difference between $ExecutionContext.SessionState.Path.CurrentLocation and $pwd in Powershell?
PS C:\> $ExecutionContext.SessionState.Path.CurrentLocationPath----C:\PS C:\> $ExecutionContext.SessionState.Path.CurrentLocation.GetType().FullNameSystem.Management.Automation.PathInfoPS C:\> $PWDPath----C:\PS C:\> $PWD.GetType().FullNameSystem.Management.Automation.PathInfo
So, basically the difference is that $ExecutionContext.SessionState.Path.CurrentLocation
requires significantly more typing than $PWD
.
It is true that $pwd
gets its value from $ExecutionContext
.
The key difference between these two variables is $pwd
can be overwritten, but $ExecutionContext
is Constant (Readonly).
$ExecutionContext
is intended to mimic the interface available to the cmdlet author. $pwd
is just a convenient way to get current path.
So, it is recommended to use $ExecutionContext
if you need to get the path without worrying anyone may messing up the value of $pwd
.
$ExecutionContext.SessionState.Path.CurrentLocation
and $PWD
actually have difference.
When you use $PWD
in fact you will get result of $ExecutionContext.SessionState.Path.CurrentLocation
,
whereas if you use $ExecutionContext
you will get more property about execution context .