Monthly Archive

Categories

Location, location

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

cd C:\test\ 
.\hello.ps1 
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  3.1.0.0 Microsoft.PowerShell.Management 
Cmdlet      Pop-Location  3.1.0.0 Microsoft.PowerShell.Management 
Cmdlet      Push-Location 3.1.0.0 Microsoft.PowerShell.Management 
Cmdlet      Set-Location  3.1.0.0 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

Path 
---- 
C:\Scripts

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

Path 
---- 
C:\test

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

PS> Get-Location

Path 
---- 
C:\Scripts

Saves all that remembering stuff

Comments are closed.