header image

Location, location

Posted by: | June 28, 2017 Comments Off on Location, location |

Just recently I’ve found my self repeatedly working through a location, location pattern.

cd C:\test\ 
cd C:\Scripts\

The pattern consists of changing to another folder. Running some code and then changing back to the original folder – assuming you can remember it.

I then remembered the location cmdlets

PS> Get-Command *-Location | ft -a

CommandType Name          Version Source 
----------- ----          ------- ------ 
Cmdlet      Get-Location Microsoft.PowerShell.Management 
Cmdlet      Pop-Location Microsoft.PowerShell.Management 
Cmdlet      Push-Location Microsoft.PowerShell.Management 
Cmdlet      Set-Location Microsoft.PowerShell.Management

The 2 useful ones in this working pattern are Push-Location and Pop-Location

Push-Location adds the current location to the location stack and moves you to a new location if you supply a path

Pop-Location pulls the topmost location from the location stack and moves you to that location

The pattern then becomes

PS> Get-Location


PS> Push-Location -Path C:\test\ 
PS> Get-Location


PS> .\hello.ps1 
Hello world! 
PS> Pop-Location

PS> Get-Location


Saves all that remembering stuff

under: PowerShell

Comments are closed.