5

As far as I can see using $pwd and using ./ in PowerShell gives the same result.

Are they the same or is there a difference?

Clijsters
  • 4,031
  • 1
  • 27
  • 37
J. Doe
  • 1,147
  • 1
  • 13
  • 21

2 Answers2

10

$pwd is an automatic variable. Its name stands for "present working directory" and it should always contain the current working path.

You can use just "." or ".\" as a path parameter to represent the current location, but you can't set a variable to .\ and have it then contain the current path, so in this regard they are different.

As an example if you were writing some script logic that needed to check the current working directory you would need to use $pwd vs .\. For example:

if ($pwd -eq 'c:\some\path') { }

Would work. However:

if (. -eq 'c:\some\path') { }

Would not.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
5

$PWD and Get-Location (alias: pwd) give you an absolute path, . is a relative path. This can be important when storing a path to re-use later (e.g. in a different location), where . will always be relative to the current location.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • While I understand what you mean in my experiments "$pwd" and "./" give the same output. – J. Doe Feb 22 '18 at 09:05
  • Those are two very different strings; I cannot fathom how they can result in the same output. – Joey Feb 22 '18 at 09:30
  • Well, yes, for `Resolve-Path` the result is the same. But you chose to omit that tidbit from your comment ;-) I just noticed that `Resolve-Path` doesn't work like I thought it would anyway; I omitted that part again. – Joey Feb 22 '18 at 10:24
  • Oh... sorry. I just don't have enough experience with more complex scripts and for what I know the end result is like the screenshot. But if you say they are different - I believe it. – J. Doe Feb 22 '18 at 12:56