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

This entry was posted in Powershell. Bookmark the permalink.

3 Responses to Location, location

  1. fullenw1 says:

    And after several push, when you don’t remember where you have been before there’s Get-Location -Stack.
    Thanks Richard!

  2. cavallogolooso says:

    remember that you could do all the CD stuff with pushd / popd (also with UNC, also nested) .. but here we are with powershell! 🙂

  3. davecarnahan says:

    Yes, cavallogolooso is right.
    Take a UNC path, feed it to the PUSHD cmdlet and PowerShell (and even DOS) assign a drive letter to the area and place your shell session at this location.

    Like DOS, the order of the drive letter assigned is from Z: back down.

    What’s also nice about this is that afterwards, PowerShell has made a static drive letter / folder out of this area, where many applications still need this type of reference
    (and won’t work given a UNC address e.g. \\somewhere\somefolder, apps need a M:\myfolder drive reference).

    Then when your PowerShell session has ended, the drive letter is gone and cleaned up.

    A much better approach to drive letter management because you’d easily use up your drive letters using the ‘NET USE’ technique.

    With NET USE, each use is best to clean up your drive directly after you’re done with the area, because this drive letter will linger around to the next session. It’s best to make a fresh start out of the whole deal each time, instead of guessing if your mapped drive still exists.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s