PowerShell for Windows Admins

Jun 28 2017   12:42PM GMT

Location, location

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: