11

At first sight Set-Location and Push-Location PowerShell cmdlets look alike when it comes to changing the current directory.

What are use cases for each? When would one choose to use one over the other?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jacek M
  • 2,349
  • 5
  • 22
  • 47
  • push-location acts like a sort of stack of path, you can then use pop-location to revert to a previous saved path. The doc : https://technet.microsoft.com/en-us/library/hh849855.aspx – Loïc MICHEL Sep 23 '15 at 08:23
  • Why would someone use Set-Location then, if Push-Location gives you the same functionality with extra stack of paths feature? :) – Jacek M Sep 23 '15 at 08:25
  • because you may not want go back to a previous dir, and I think set-location will consume less ressource – Loïc MICHEL Sep 23 '15 at 08:26
  • Fair enough, yet resource consumption sounds like a lousy enough reason to have both. I feel it still deserves a nice SO answer. – Jacek M Sep 23 '15 at 08:29
  • I think it's only a convenience and a legacy of the cd, pushd command of cmd.exe ... – Loïc MICHEL Sep 23 '15 at 08:35

1 Answers1

25

They will both change the working directory but using Push-Location will add the current working directory to the top of a stack before changing to the new working directory. You can then use Pop-Location to traverse back down the stack through the previous working directories.

Set-Location will just change the working directory in the same way that cd would.

Set-Location = cd
Push-Location = pushd
Pop-Location = popd
arco444
  • 22,002
  • 12
  • 63
  • 67
  • 3
    One problem is that `Push-Location` does not change `[Environment]::CurrentDirectory` in the console host, but does in the ISE host. https://stackoverflow.com/a/56462392/447901 – lit Jun 05 '19 at 14:19